{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 1: Getting Started with Python Polars "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The Polars DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import polars as pl"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 2)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>nums</th><th>letters</th></tr><tr><td>i64</td><td>str</td></tr></thead><tbody><tr><td>1</td><td>&quot;a&quot;</td></tr><tr><td>2</td><td>&quot;b&quot;</td></tr><tr><td>3</td><td>&quot;c&quot;</td></tr><tr><td>4</td><td>&quot;d&quot;</td></tr><tr><td>5</td><td>&quot;e&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 2)\n",
       "┌──────┬─────────┐\n",
       "│ nums ┆ letters │\n",
       "│ ---  ┆ ---     │\n",
       "│ i64  ┆ str     │\n",
       "╞══════╪═════════╡\n",
       "│ 1    ┆ a       │\n",
       "│ 2    ┆ b       │\n",
       "│ 3    ┆ c       │\n",
       "│ 4    ┆ d       │\n",
       "│ 5    ┆ e       │\n",
       "└──────┴─────────┘"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pl.DataFrame({\n",
    "    'nums': [1,2,3,4,5],\n",
    "    'letters': ['a','b','c','d','e']\n",
    "})\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 12)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>PassengerId</th><th>Survived</th><th>Pclass</th><th>Name</th><th>Sex</th><th>Age</th><th>SibSp</th><th>Parch</th><th>Ticket</th><th>Fare</th><th>Cabin</th><th>Embarked</th></tr><tr><td>i64</td><td>i64</td><td>i64</td><td>str</td><td>str</td><td>f64</td><td>i64</td><td>i64</td><td>str</td><td>f64</td><td>str</td><td>str</td></tr></thead><tbody><tr><td>1</td><td>0</td><td>3</td><td>&quot;Braund, Mr. Owen Harris&quot;</td><td>&quot;male&quot;</td><td>22.0</td><td>1</td><td>0</td><td>&quot;A/5 21171&quot;</td><td>7.25</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>2</td><td>1</td><td>1</td><td>&quot;Cumings, Mrs. John Bradley (Fl…</td><td>&quot;female&quot;</td><td>38.0</td><td>1</td><td>0</td><td>&quot;PC 17599&quot;</td><td>71.2833</td><td>&quot;C85&quot;</td><td>&quot;C&quot;</td></tr><tr><td>3</td><td>1</td><td>3</td><td>&quot;Heikkinen, Miss. Laina&quot;</td><td>&quot;female&quot;</td><td>26.0</td><td>0</td><td>0</td><td>&quot;STON/O2. 3101282&quot;</td><td>7.925</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>4</td><td>1</td><td>1</td><td>&quot;Futrelle, Mrs. Jacques Heath (…</td><td>&quot;female&quot;</td><td>35.0</td><td>1</td><td>0</td><td>&quot;113803&quot;</td><td>53.1</td><td>&quot;C123&quot;</td><td>&quot;S&quot;</td></tr><tr><td>5</td><td>0</td><td>3</td><td>&quot;Allen, Mr. William Henry&quot;</td><td>&quot;male&quot;</td><td>35.0</td><td>0</td><td>0</td><td>&quot;373450&quot;</td><td>8.05</td><td>null</td><td>&quot;S&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 12)\n",
       "┌─────────────┬──────────┬────────┬───────────────────┬───┬───────────┬─────────┬───────┬──────────┐\n",
       "│ PassengerId ┆ Survived ┆ Pclass ┆ Name              ┆ … ┆ Ticket    ┆ Fare    ┆ Cabin ┆ Embarked │\n",
       "│ ---         ┆ ---      ┆ ---    ┆ ---               ┆   ┆ ---       ┆ ---     ┆ ---   ┆ ---      │\n",
       "│ i64         ┆ i64      ┆ i64    ┆ str               ┆   ┆ str       ┆ f64     ┆ str   ┆ str      │\n",
       "╞═════════════╪══════════╪════════╪═══════════════════╪═══╪═══════════╪═════════╪═══════╪══════════╡\n",
       "│ 1           ┆ 0        ┆ 3      ┆ Braund, Mr. Owen  ┆ … ┆ A/5 21171 ┆ 7.25    ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Harris            ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 2           ┆ 1        ┆ 1      ┆ Cumings, Mrs.     ┆ … ┆ PC 17599  ┆ 71.2833 ┆ C85   ┆ C        │\n",
       "│             ┆          ┆        ┆ John Bradley (Fl… ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 3           ┆ 1        ┆ 3      ┆ Heikkinen, Miss.  ┆ … ┆ STON/O2.  ┆ 7.925   ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Laina             ┆   ┆ 3101282   ┆         ┆       ┆          │\n",
       "│ 4           ┆ 1        ┆ 1      ┆ Futrelle, Mrs.    ┆ … ┆ 113803    ┆ 53.1    ┆ C123  ┆ S        │\n",
       "│             ┆          ┆        ┆ Jacques Heath (…  ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 5           ┆ 0        ┆ 3      ┆ Allen, Mr.        ┆ … ┆ 373450    ┆ 8.05    ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ William Henry     ┆   ┆           ┆         ┆       ┆          │\n",
       "└─────────────┴──────────┴────────┴───────────────────┴───┴───────────┴─────────┴───────┴──────────┘"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pl.read_csv('../data/titanic_dataset.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Schema([('PassengerId', Int64),\n",
       "        ('Survived', Int64),\n",
       "        ('Pclass', Int64),\n",
       "        ('Name', String),\n",
       "        ('Sex', String),\n",
       "        ('Age', Float64),\n",
       "        ('SibSp', Int64),\n",
       "        ('Parch', Int64),\n",
       "        ('Ticket', String),\n",
       "        ('Fare', Float64),\n",
       "        ('Cabin', String),\n",
       "        ('Embarked', String)])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.schema"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['PassengerId',\n",
       " 'Survived',\n",
       " 'Pclass',\n",
       " 'Name',\n",
       " 'Sex',\n",
       " 'Age',\n",
       " 'SibSp',\n",
       " 'Parch',\n",
       " 'Ticket',\n",
       " 'Fare',\n",
       " 'Cabin',\n",
       " 'Embarked']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Int64,\n",
       " Int64,\n",
       " Int64,\n",
       " String,\n",
       " String,\n",
       " Float64,\n",
       " Int64,\n",
       " Int64,\n",
       " String,\n",
       " Float64,\n",
       " String,\n",
       " String]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(891, 12)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "891"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.height"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.width"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'PassengerId': {'SORTED_ASC': False, 'SORTED_DESC': False},\n",
       " 'Survived': {'SORTED_ASC': False, 'SORTED_DESC': False},\n",
       " 'Pclass': {'SORTED_ASC': False, 'SORTED_DESC': False},\n",
       " 'Name': {'SORTED_ASC': False, 'SORTED_DESC': False},\n",
       " 'Sex': {'SORTED_ASC': False, 'SORTED_DESC': False},\n",
       " 'Age': {'SORTED_ASC': False, 'SORTED_DESC': False},\n",
       " 'SibSp': {'SORTED_ASC': False, 'SORTED_DESC': False},\n",
       " 'Parch': {'SORTED_ASC': False, 'SORTED_DESC': False},\n",
       " 'Ticket': {'SORTED_ASC': False, 'SORTED_DESC': False},\n",
       " 'Fare': {'SORTED_ASC': False, 'SORTED_DESC': False},\n",
       " 'Cabin': {'SORTED_ASC': False, 'SORTED_DESC': False},\n",
       " 'Embarked': {'SORTED_ASC': False, 'SORTED_DESC': False}}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.flags"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There is more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (3, 2)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>ones</th><th>twos</th></tr><tr><td>f32</td><td>i8</td></tr></thead><tbody><tr><td>1.0</td><td>2</td></tr><tr><td>1.0</td><td>2</td></tr><tr><td>1.0</td><td>2</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (3, 2)\n",
       "┌──────┬──────┐\n",
       "│ ones ┆ twos │\n",
       "│ ---  ┆ ---  │\n",
       "│ f32  ┆ i8   │\n",
       "╞══════╪══════╡\n",
       "│ 1.0  ┆ 2    │\n",
       "│ 1.0  ┆ 2    │\n",
       "│ 1.0  ┆ 2    │\n",
       "└──────┴──────┘"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "numpy_arr = np.array([[1,1,1], [2,2,2]])\n",
    "df = pl.from_numpy(numpy_arr, schema={'ones': pl.Float32, 'twos': pl.Int8}, orient='col') \n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The Polars Series"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5,)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>col</th></tr><tr><td>i64</td></tr></thead><tbody><tr><td>1</td></tr><tr><td>2</td></tr><tr><td>3</td></tr><tr><td>4</td></tr><tr><td>5</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5,)\n",
       "Series: 'col' [i64]\n",
       "[\n",
       "\t1\n",
       "\t2\n",
       "\t3\n",
       "\t4\n",
       "\t5\n",
       "]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pl.Series('col', [1,2,3,4,5])\n",
    "s.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (3,)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>a</th></tr><tr><td>i64</td></tr></thead><tbody><tr><td>1</td></tr><tr><td>2</td></tr><tr><td>3</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (3,)\n",
       "Series: 'a' [i64]\n",
       "[\n",
       "\t1\n",
       "\t2\n",
       "\t3\n",
       "]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = {'a': [1,2,3], 'b': [4,5,6]}\n",
    "s_a = (\n",
    "    pl.DataFrame(data)\n",
    "    .to_series()\n",
    ")\n",
    "s_a.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (3,)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>b</th></tr><tr><td>i64</td></tr></thead><tbody><tr><td>4</td></tr><tr><td>5</td></tr><tr><td>6</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (3,)\n",
       "Series: 'b' [i64]\n",
       "[\n",
       "\t4\n",
       "\t5\n",
       "\t6\n",
       "]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s_b = (\n",
    "    pl.DataFrame(data)\n",
    "    .to_series(1)\n",
    ")\n",
    "s_b.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (3,)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>b</th></tr><tr><td>i64</td></tr></thead><tbody><tr><td>4</td></tr><tr><td>5</td></tr><tr><td>6</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (3,)\n",
       "Series: 'b' [i64]\n",
       "[\n",
       "\t4\n",
       "\t5\n",
       "\t6\n",
       "]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s_b2 = (\n",
    "    pl.DataFrame(data)\n",
    "    .get_column('b')\n",
    ")\n",
    "s_b2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5,)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'col'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Int64"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.dtype"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The Polars LazyFrame"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "polars.lazyframe.frame.LazyFrame"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = {'name': ['Sarah',  'Mike', 'Bob', 'Ashley']}\n",
    "lf = pl.LazyFrame(data) \n",
    "type(lf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (4, 1)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>name</th></tr><tr><td>str</td></tr></thead><tbody><tr><td>&quot;Sarah&quot;</td></tr><tr><td>&quot;Mike&quot;</td></tr><tr><td>&quot;Bob&quot;</td></tr><tr><td>&quot;Ashley&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (4, 1)\n",
       "┌────────┐\n",
       "│ name   │\n",
       "│ ---    │\n",
       "│ str    │\n",
       "╞════════╡\n",
       "│ Sarah  │\n",
       "│ Mike   │\n",
       "│ Bob    │\n",
       "│ Ashley │\n",
       "└────────┘"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lf.collect().head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 12)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>PassengerId</th><th>Survived</th><th>Pclass</th><th>Name</th><th>Sex</th><th>Age</th><th>SibSp</th><th>Parch</th><th>Ticket</th><th>Fare</th><th>Cabin</th><th>Embarked</th></tr><tr><td>i64</td><td>i64</td><td>i64</td><td>str</td><td>str</td><td>f64</td><td>i64</td><td>i64</td><td>str</td><td>f64</td><td>str</td><td>str</td></tr></thead><tbody><tr><td>1</td><td>0</td><td>3</td><td>&quot;Braund, Mr. Owen Harris&quot;</td><td>&quot;male&quot;</td><td>22.0</td><td>1</td><td>0</td><td>&quot;A/5 21171&quot;</td><td>7.25</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>2</td><td>1</td><td>1</td><td>&quot;Cumings, Mrs. John Bradley (Fl…</td><td>&quot;female&quot;</td><td>38.0</td><td>1</td><td>0</td><td>&quot;PC 17599&quot;</td><td>71.2833</td><td>&quot;C85&quot;</td><td>&quot;C&quot;</td></tr><tr><td>3</td><td>1</td><td>3</td><td>&quot;Heikkinen, Miss. Laina&quot;</td><td>&quot;female&quot;</td><td>26.0</td><td>0</td><td>0</td><td>&quot;STON/O2. 3101282&quot;</td><td>7.925</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>4</td><td>1</td><td>1</td><td>&quot;Futrelle, Mrs. Jacques Heath (…</td><td>&quot;female&quot;</td><td>35.0</td><td>1</td><td>0</td><td>&quot;113803&quot;</td><td>53.1</td><td>&quot;C123&quot;</td><td>&quot;S&quot;</td></tr><tr><td>5</td><td>0</td><td>3</td><td>&quot;Allen, Mr. William Henry&quot;</td><td>&quot;male&quot;</td><td>35.0</td><td>0</td><td>0</td><td>&quot;373450&quot;</td><td>8.05</td><td>null</td><td>&quot;S&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 12)\n",
       "┌─────────────┬──────────┬────────┬───────────────────┬───┬───────────┬─────────┬───────┬──────────┐\n",
       "│ PassengerId ┆ Survived ┆ Pclass ┆ Name              ┆ … ┆ Ticket    ┆ Fare    ┆ Cabin ┆ Embarked │\n",
       "│ ---         ┆ ---      ┆ ---    ┆ ---               ┆   ┆ ---       ┆ ---     ┆ ---   ┆ ---      │\n",
       "│ i64         ┆ i64      ┆ i64    ┆ str               ┆   ┆ str       ┆ f64     ┆ str   ┆ str      │\n",
       "╞═════════════╪══════════╪════════╪═══════════════════╪═══╪═══════════╪═════════╪═══════╪══════════╡\n",
       "│ 1           ┆ 0        ┆ 3      ┆ Braund, Mr. Owen  ┆ … ┆ A/5 21171 ┆ 7.25    ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Harris            ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 2           ┆ 1        ┆ 1      ┆ Cumings, Mrs.     ┆ … ┆ PC 17599  ┆ 71.2833 ┆ C85   ┆ C        │\n",
       "│             ┆          ┆        ┆ John Bradley (Fl… ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 3           ┆ 1        ┆ 3      ┆ Heikkinen, Miss.  ┆ … ┆ STON/O2.  ┆ 7.925   ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Laina             ┆   ┆ 3101282   ┆         ┆       ┆          │\n",
       "│ 4           ┆ 1        ┆ 1      ┆ Futrelle, Mrs.    ┆ … ┆ 113803    ┆ 53.1    ┆ C123  ┆ S        │\n",
       "│             ┆          ┆        ┆ Jacques Heath (…  ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 5           ┆ 0        ┆ 3      ┆ Allen, Mr.        ┆ … ┆ 373450    ┆ 8.05    ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ William Henry     ┆   ┆           ┆         ┆       ┆          │\n",
       "└─────────────┴──────────┴────────┴───────────────────┴───┴───────────┴─────────┴───────┴──────────┘"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lf = pl.scan_csv('../data/titanic_dataset.csv')\n",
    "lf.head().collect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (3, 12)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>PassengerId</th><th>Survived</th><th>Pclass</th><th>Name</th><th>Sex</th><th>Age</th><th>SibSp</th><th>Parch</th><th>Ticket</th><th>Fare</th><th>Cabin</th><th>Embarked</th></tr><tr><td>i64</td><td>i64</td><td>i64</td><td>str</td><td>str</td><td>f64</td><td>i64</td><td>i64</td><td>str</td><td>f64</td><td>str</td><td>str</td></tr></thead><tbody><tr><td>1</td><td>0</td><td>3</td><td>&quot;Braund, Mr. Owen Harris&quot;</td><td>&quot;male&quot;</td><td>22.0</td><td>1</td><td>0</td><td>&quot;A/5 21171&quot;</td><td>7.25</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>2</td><td>1</td><td>1</td><td>&quot;Cumings, Mrs. John Bradley (Fl…</td><td>&quot;female&quot;</td><td>38.0</td><td>1</td><td>0</td><td>&quot;PC 17599&quot;</td><td>71.2833</td><td>&quot;C85&quot;</td><td>&quot;C&quot;</td></tr><tr><td>3</td><td>1</td><td>3</td><td>&quot;Heikkinen, Miss. Laina&quot;</td><td>&quot;female&quot;</td><td>26.0</td><td>0</td><td>0</td><td>&quot;STON/O2. 3101282&quot;</td><td>7.925</td><td>null</td><td>&quot;S&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (3, 12)\n",
       "┌─────────────┬──────────┬────────┬───────────────────┬───┬───────────┬─────────┬───────┬──────────┐\n",
       "│ PassengerId ┆ Survived ┆ Pclass ┆ Name              ┆ … ┆ Ticket    ┆ Fare    ┆ Cabin ┆ Embarked │\n",
       "│ ---         ┆ ---      ┆ ---    ┆ ---               ┆   ┆ ---       ┆ ---     ┆ ---   ┆ ---      │\n",
       "│ i64         ┆ i64      ┆ i64    ┆ str               ┆   ┆ str       ┆ f64     ┆ str   ┆ str      │\n",
       "╞═════════════╪══════════╪════════╪═══════════════════╪═══╪═══════════╪═════════╪═══════╪══════════╡\n",
       "│ 1           ┆ 0        ┆ 3      ┆ Braund, Mr. Owen  ┆ … ┆ A/5 21171 ┆ 7.25    ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Harris            ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 2           ┆ 1        ┆ 1      ┆ Cumings, Mrs.     ┆ … ┆ PC 17599  ┆ 71.2833 ┆ C85   ┆ C        │\n",
       "│             ┆          ┆        ┆ John Bradley (Fl… ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 3           ┆ 1        ┆ 3      ┆ Heikkinen, Miss.  ┆ … ┆ STON/O2.  ┆ 7.925   ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Laina             ┆   ┆ 3101282   ┆         ┆       ┆          │\n",
       "└─────────────┴──────────┴────────┴───────────────────┴───┴───────────┴─────────┴───────┴──────────┘"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pl.read_csv('../data/titanic_dataset.csv')\n",
    "df.lazy().head(3).collect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Schema([('PassengerId', Int64),\n",
       "        ('Survived', Int64),\n",
       "        ('Pclass', Int64),\n",
       "        ('Name', String),\n",
       "        ('Sex', String),\n",
       "        ('Age', Float64),\n",
       "        ('SibSp', Int64),\n",
       "        ('Parch', Int64),\n",
       "        ('Ticket', String),\n",
       "        ('Fare', Float64),\n",
       "        ('Cabin', String),\n",
       "        ('Embarked', String)])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lf.collect_schema()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lf.collect_schema().len()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"230pt\" height=\"138pt\" viewBox=\"0.00 0.00 229.50 137.50\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 133.5)\">\n",
       "<title>polars_query</title>\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-133.5 225.5,-133.5 225.5,4 -4,4\"/>\n",
       "<!-- FAST_PROJECT: [Name, Age] [(0, 0)] -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>FAST_PROJECT: [Name, Age] [(0, 0)]</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"206.5,-129.5 15,-129.5 15,-93.5 206.5,-93.5 206.5,-129.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"110.75\" y=\"-106.45\" font-family=\"Times,serif\" font-size=\"14.00\">FAST_PROJECT: [Name, Age]</text>\n",
       "</g>\n",
       "<!-- Csv SCAN ../data/titanic_dataset.csv;\n",
       "π 2/12;\n",
       "σ &#45; [(0, 1)] -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>Csv SCAN ../data/titanic_dataset.csv;\n",
       "π 2/12;\n",
       "σ - [(0, 1)]</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"221.5,-57.5 0,-57.5 0,0 221.5,0 221.5,-57.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"110.75\" y=\"-40.2\" font-family=\"Times,serif\" font-size=\"14.00\">Csv SCAN ../data/titanic_dataset.csv;</text>\n",
       "<text text-anchor=\"middle\" x=\"110.75\" y=\"-23.7\" font-family=\"Times,serif\" font-size=\"14.00\">π 2/12;</text>\n",
       "<text text-anchor=\"middle\" x=\"110.75\" y=\"-7.2\" font-family=\"Times,serif\" font-size=\"14.00\">σ -</text>\n",
       "</g>\n",
       "<!-- FAST_PROJECT: [Name, Age] [(0, 0)]&#45;&#45;Csv SCAN ../data/titanic_dataset.csv;\n",
       "π 2/12;\n",
       "σ &#45; [(0, 1)] -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>FAST_PROJECT: [Name, Age] [(0, 0)]--Csv SCAN ../data/titanic_dataset.csv;\n",
       "π 2/12;\n",
       "σ - [(0, 1)]</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M110.75,-93.38C110.75,-83.1 110.75,-69.77 110.75,-57.97\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "(\n",
    "    lf\n",
    "    .select(pl.col('Name', 'Age'))\n",
    "    .show_graph()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"230pt\" height=\"138pt\" viewBox=\"0.00 0.00 229.50 137.50\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 133.5)\">\n",
       "<title>polars_query</title>\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-133.5 225.5,-133.5 225.5,4 -4,4\"/>\n",
       "<!-- π 2/12 [(0, 0)] -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>π 2/12 [(0, 0)]</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"137.75,-129.5 83.75,-129.5 83.75,-93.5 137.75,-93.5 137.75,-129.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"110.75\" y=\"-106.45\" font-family=\"Times,serif\" font-size=\"14.00\">π 2/12</text>\n",
       "</g>\n",
       "<!-- Csv SCAN ../data/titanic_dataset.csv;\n",
       "π */12;\n",
       "σ &#45; [(0, 1)] -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>Csv SCAN ../data/titanic_dataset.csv;\n",
       "π */12;\n",
       "σ - [(0, 1)]</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"221.5,-57.5 0,-57.5 0,0 221.5,0 221.5,-57.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"110.75\" y=\"-40.2\" font-family=\"Times,serif\" font-size=\"14.00\">Csv SCAN ../data/titanic_dataset.csv;</text>\n",
       "<text text-anchor=\"middle\" x=\"110.75\" y=\"-23.7\" font-family=\"Times,serif\" font-size=\"14.00\">π */12;</text>\n",
       "<text text-anchor=\"middle\" x=\"110.75\" y=\"-7.2\" font-family=\"Times,serif\" font-size=\"14.00\">σ -</text>\n",
       "</g>\n",
       "<!-- π 2/12 [(0, 0)]&#45;&#45;Csv SCAN ../data/titanic_dataset.csv;\n",
       "π */12;\n",
       "σ &#45; [(0, 1)] -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>π 2/12 [(0, 0)]--Csv SCAN ../data/titanic_dataset.csv;\n",
       "π */12;\n",
       "σ - [(0, 1)]</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M110.75,-93.38C110.75,-83.1 110.75,-69.77 110.75,-57.97\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "(\n",
    "    lf\n",
    "    .select(pl.col('Name', 'Age'))\n",
    "    .show_graph(optimized=False)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'FAST_PROJECT: [Name, Age]\\n\\n    Csv SCAN ../data/titanic_dataset.csv\\n    PROJECT 2/12 COLUMNS'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(\n",
    "    lf\n",
    "    .select(pl.col('Name', 'Age'))\n",
    "    .explain()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FAST_PROJECT: [Name, Age]\n",
      "\n",
      "    Csv SCAN ../data/titanic_dataset.csv\n",
      "    PROJECT 2/12 COLUMNS\n"
     ]
    }
   ],
   "source": [
    "print(\n",
    "    lf\n",
    "    .select(pl.col('Name', 'Age'))\n",
    "    .explain()\n",
    "    , sep='\\n'\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Selecting columns and filtering data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 12)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>PassengerId</th><th>Survived</th><th>Pclass</th><th>Name</th><th>Sex</th><th>Age</th><th>SibSp</th><th>Parch</th><th>Ticket</th><th>Fare</th><th>Cabin</th><th>Embarked</th></tr><tr><td>i64</td><td>i64</td><td>i64</td><td>str</td><td>str</td><td>f64</td><td>i64</td><td>i64</td><td>str</td><td>f64</td><td>str</td><td>str</td></tr></thead><tbody><tr><td>1</td><td>0</td><td>3</td><td>&quot;Braund, Mr. Ow…</td><td>&quot;male&quot;</td><td>22.0</td><td>1</td><td>0</td><td>&quot;A/5 21171&quot;</td><td>7.25</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>2</td><td>1</td><td>1</td><td>&quot;Cumings, Mrs. …</td><td>&quot;female&quot;</td><td>38.0</td><td>1</td><td>0</td><td>&quot;PC 17599&quot;</td><td>71.2833</td><td>&quot;C85&quot;</td><td>&quot;C&quot;</td></tr><tr><td>3</td><td>1</td><td>3</td><td>&quot;Heikkinen, Mis…</td><td>&quot;female&quot;</td><td>26.0</td><td>0</td><td>0</td><td>&quot;STON/O2. 31012…</td><td>7.925</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>4</td><td>1</td><td>1</td><td>&quot;Futrelle, Mrs.…</td><td>&quot;female&quot;</td><td>35.0</td><td>1</td><td>0</td><td>&quot;113803&quot;</td><td>53.1</td><td>&quot;C123&quot;</td><td>&quot;S&quot;</td></tr><tr><td>5</td><td>0</td><td>3</td><td>&quot;Allen, Mr. Wil…</td><td>&quot;male&quot;</td><td>35.0</td><td>0</td><td>0</td><td>&quot;373450&quot;</td><td>8.05</td><td>null</td><td>&quot;S&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 12)\n",
       "┌─────────────┬──────────┬────────┬───────────────────┬───┬───────────┬─────────┬───────┬──────────┐\n",
       "│ PassengerId ┆ Survived ┆ Pclass ┆ Name              ┆ … ┆ Ticket    ┆ Fare    ┆ Cabin ┆ Embarked │\n",
       "│ ---         ┆ ---      ┆ ---    ┆ ---               ┆   ┆ ---       ┆ ---     ┆ ---   ┆ ---      │\n",
       "│ i64         ┆ i64      ┆ i64    ┆ str               ┆   ┆ str       ┆ f64     ┆ str   ┆ str      │\n",
       "╞═════════════╪══════════╪════════╪═══════════════════╪═══╪═══════════╪═════════╪═══════╪══════════╡\n",
       "│ 1           ┆ 0        ┆ 3      ┆ Braund, Mr. Owen  ┆ … ┆ A/5 21171 ┆ 7.25    ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Harris            ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 2           ┆ 1        ┆ 1      ┆ Cumings, Mrs.     ┆ … ┆ PC 17599  ┆ 71.2833 ┆ C85   ┆ C        │\n",
       "│             ┆          ┆        ┆ John Bradley      ┆   ┆           ┆         ┆       ┆          │\n",
       "│             ┆          ┆        ┆ (Flor…            ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 3           ┆ 1        ┆ 3      ┆ Heikkinen, Miss.  ┆ … ┆ STON/O2.  ┆ 7.925   ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Laina             ┆   ┆ 3101282   ┆         ┆       ┆          │\n",
       "│ 4           ┆ 1        ┆ 1      ┆ Futrelle, Mrs.    ┆ … ┆ 113803    ┆ 53.1    ┆ C123  ┆ S        │\n",
       "│             ┆          ┆        ┆ Jacques Heath     ┆   ┆           ┆         ┆       ┆          │\n",
       "│             ┆          ┆        ┆ (Li…              ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 5           ┆ 0        ┆ 3      ┆ Allen, Mr.        ┆ … ┆ 373450    ┆ 8.05    ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ William Henry     ┆   ┆           ┆         ┆       ┆          │\n",
       "└─────────────┴──────────┴────────┴───────────────────┴───┴───────────┴─────────┴───────┴──────────┘"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pl.read_csv('../data/titanic_dataset.csv') \n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 3)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Survived</th><th>Ticket</th><th>Fare</th></tr><tr><td>i64</td><td>str</td><td>f64</td></tr></thead><tbody><tr><td>0</td><td>&quot;A/5 21171&quot;</td><td>7.25</td></tr><tr><td>1</td><td>&quot;PC 17599&quot;</td><td>71.2833</td></tr><tr><td>1</td><td>&quot;STON/O2. 31012…</td><td>7.925</td></tr><tr><td>1</td><td>&quot;113803&quot;</td><td>53.1</td></tr><tr><td>0</td><td>&quot;373450&quot;</td><td>8.05</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 3)\n",
       "┌──────────┬──────────────────┬─────────┐\n",
       "│ Survived ┆ Ticket           ┆ Fare    │\n",
       "│ ---      ┆ ---              ┆ ---     │\n",
       "│ i64      ┆ str              ┆ f64     │\n",
       "╞══════════╪══════════════════╪═════════╡\n",
       "│ 0        ┆ A/5 21171        ┆ 7.25    │\n",
       "│ 1        ┆ PC 17599         ┆ 71.2833 │\n",
       "│ 1        ┆ STON/O2. 3101282 ┆ 7.925   │\n",
       "│ 1        ┆ 113803           ┆ 53.1    │\n",
       "│ 0        ┆ 373450           ┆ 8.05    │\n",
       "└──────────┴──────────────────┴─────────┘"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.select(['Survived', 'Ticket', 'Fare']).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 3)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Survived</th><th>Ticket</th><th>Fare</th></tr><tr><td>i64</td><td>str</td><td>f64</td></tr></thead><tbody><tr><td>0</td><td>&quot;A/5 21171&quot;</td><td>7.25</td></tr><tr><td>1</td><td>&quot;PC 17599&quot;</td><td>71.2833</td></tr><tr><td>1</td><td>&quot;STON/O2. 31012…</td><td>7.925</td></tr><tr><td>1</td><td>&quot;113803&quot;</td><td>53.1</td></tr><tr><td>0</td><td>&quot;373450&quot;</td><td>8.05</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 3)\n",
       "┌──────────┬──────────────────┬─────────┐\n",
       "│ Survived ┆ Ticket           ┆ Fare    │\n",
       "│ ---      ┆ ---              ┆ ---     │\n",
       "│ i64      ┆ str              ┆ f64     │\n",
       "╞══════════╪══════════════════╪═════════╡\n",
       "│ 0        ┆ A/5 21171        ┆ 7.25    │\n",
       "│ 1        ┆ PC 17599         ┆ 71.2833 │\n",
       "│ 1        ┆ STON/O2. 3101282 ┆ 7.925   │\n",
       "│ 1        ┆ 113803           ┆ 53.1    │\n",
       "│ 0        ┆ 373450           ┆ 8.05    │\n",
       "└──────────┴──────────────────┴─────────┘"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.select(pl.col(['Survived', 'Ticket', 'Fare'])).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 3)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Survived</th><th>Ticket</th><th>Fare</th></tr><tr><td>i64</td><td>str</td><td>f64</td></tr></thead><tbody><tr><td>0</td><td>&quot;A/5 21171&quot;</td><td>7.25</td></tr><tr><td>1</td><td>&quot;PC 17599&quot;</td><td>71.2833</td></tr><tr><td>1</td><td>&quot;STON/O2. 31012…</td><td>7.925</td></tr><tr><td>1</td><td>&quot;113803&quot;</td><td>53.1</td></tr><tr><td>0</td><td>&quot;373450&quot;</td><td>8.05</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 3)\n",
       "┌──────────┬──────────────────┬─────────┐\n",
       "│ Survived ┆ Ticket           ┆ Fare    │\n",
       "│ ---      ┆ ---              ┆ ---     │\n",
       "│ i64      ┆ str              ┆ f64     │\n",
       "╞══════════╪══════════════════╪═════════╡\n",
       "│ 0        ┆ A/5 21171        ┆ 7.25    │\n",
       "│ 1        ┆ PC 17599         ┆ 71.2833 │\n",
       "│ 1        ┆ STON/O2. 3101282 ┆ 7.925   │\n",
       "│ 1        ┆ 113803           ┆ 53.1    │\n",
       "│ 0        ┆ 373450           ┆ 8.05    │\n",
       "└──────────┴──────────────────┴─────────┘"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.select(\n",
    "    pl.col('Survived'),\n",
    "    pl.col('Ticket'),\n",
    "    pl.col('Fare')\n",
    ").head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 12)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>PassengerId</th><th>Survived</th><th>Pclass</th><th>Name</th><th>Sex</th><th>Age</th><th>SibSp</th><th>Parch</th><th>Ticket</th><th>Fare</th><th>Cabin</th><th>Embarked</th></tr><tr><td>i64</td><td>i64</td><td>i64</td><td>str</td><td>str</td><td>f64</td><td>i64</td><td>i64</td><td>str</td><td>f64</td><td>str</td><td>str</td></tr></thead><tbody><tr><td>1</td><td>0</td><td>3</td><td>&quot;Braund, Mr. Ow…</td><td>&quot;male&quot;</td><td>22.0</td><td>1</td><td>0</td><td>&quot;A/5 21171&quot;</td><td>7.25</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>2</td><td>1</td><td>1</td><td>&quot;Cumings, Mrs. …</td><td>&quot;female&quot;</td><td>38.0</td><td>1</td><td>0</td><td>&quot;PC 17599&quot;</td><td>71.2833</td><td>&quot;C85&quot;</td><td>&quot;C&quot;</td></tr><tr><td>3</td><td>1</td><td>3</td><td>&quot;Heikkinen, Mis…</td><td>&quot;female&quot;</td><td>26.0</td><td>0</td><td>0</td><td>&quot;STON/O2. 31012…</td><td>7.925</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>4</td><td>1</td><td>1</td><td>&quot;Futrelle, Mrs.…</td><td>&quot;female&quot;</td><td>35.0</td><td>1</td><td>0</td><td>&quot;113803&quot;</td><td>53.1</td><td>&quot;C123&quot;</td><td>&quot;S&quot;</td></tr><tr><td>5</td><td>0</td><td>3</td><td>&quot;Allen, Mr. Wil…</td><td>&quot;male&quot;</td><td>35.0</td><td>0</td><td>0</td><td>&quot;373450&quot;</td><td>8.05</td><td>null</td><td>&quot;S&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 12)\n",
       "┌─────────────┬──────────┬────────┬───────────────────┬───┬───────────┬─────────┬───────┬──────────┐\n",
       "│ PassengerId ┆ Survived ┆ Pclass ┆ Name              ┆ … ┆ Ticket    ┆ Fare    ┆ Cabin ┆ Embarked │\n",
       "│ ---         ┆ ---      ┆ ---    ┆ ---               ┆   ┆ ---       ┆ ---     ┆ ---   ┆ ---      │\n",
       "│ i64         ┆ i64      ┆ i64    ┆ str               ┆   ┆ str       ┆ f64     ┆ str   ┆ str      │\n",
       "╞═════════════╪══════════╪════════╪═══════════════════╪═══╪═══════════╪═════════╪═══════╪══════════╡\n",
       "│ 1           ┆ 0        ┆ 3      ┆ Braund, Mr. Owen  ┆ … ┆ A/5 21171 ┆ 7.25    ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Harris            ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 2           ┆ 1        ┆ 1      ┆ Cumings, Mrs.     ┆ … ┆ PC 17599  ┆ 71.2833 ┆ C85   ┆ C        │\n",
       "│             ┆          ┆        ┆ John Bradley      ┆   ┆           ┆         ┆       ┆          │\n",
       "│             ┆          ┆        ┆ (Flor…            ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 3           ┆ 1        ┆ 3      ┆ Heikkinen, Miss.  ┆ … ┆ STON/O2.  ┆ 7.925   ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Laina             ┆   ┆ 3101282   ┆         ┆       ┆          │\n",
       "│ 4           ┆ 1        ┆ 1      ┆ Futrelle, Mrs.    ┆ … ┆ 113803    ┆ 53.1    ┆ C123  ┆ S        │\n",
       "│             ┆          ┆        ┆ Jacques Heath     ┆   ┆           ┆         ┆       ┆          │\n",
       "│             ┆          ┆        ┆ (Li…              ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 5           ┆ 0        ┆ 3      ┆ Allen, Mr.        ┆ … ┆ 373450    ┆ 8.05    ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ William Henry     ┆   ┆           ┆         ┆       ┆          │\n",
       "└─────────────┴──────────┴────────┴───────────────────┴───┴───────────┴─────────┴───────┴──────────┘"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.with_columns(\n",
    "    pl.col('Survived'),\n",
    "    pl.col('Ticket'),\n",
    "    pl.col('Fare')\n",
    ").head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 12)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>PassengerId</th><th>Survived</th><th>Pclass</th><th>Name</th><th>Sex</th><th>Age</th><th>SibSp</th><th>Parch</th><th>Ticket</th><th>Fare</th><th>Cabin</th><th>Embarked</th></tr><tr><td>i64</td><td>i64</td><td>i64</td><td>str</td><td>str</td><td>f64</td><td>i64</td><td>i64</td><td>str</td><td>f64</td><td>str</td><td>str</td></tr></thead><tbody><tr><td>1</td><td>0</td><td>3</td><td>&quot;Braund, Mr. Ow…</td><td>&quot;male&quot;</td><td>22.0</td><td>1</td><td>0</td><td>&quot;A/5 21171&quot;</td><td>7.25</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>2</td><td>1</td><td>1</td><td>&quot;Cumings, Mrs. …</td><td>&quot;female&quot;</td><td>38.0</td><td>1</td><td>0</td><td>&quot;PC 17599&quot;</td><td>71.2833</td><td>&quot;C85&quot;</td><td>&quot;C&quot;</td></tr><tr><td>3</td><td>1</td><td>3</td><td>&quot;Heikkinen, Mis…</td><td>&quot;female&quot;</td><td>26.0</td><td>0</td><td>0</td><td>&quot;STON/O2. 31012…</td><td>7.925</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>4</td><td>1</td><td>1</td><td>&quot;Futrelle, Mrs.…</td><td>&quot;female&quot;</td><td>35.0</td><td>1</td><td>0</td><td>&quot;113803&quot;</td><td>53.1</td><td>&quot;C123&quot;</td><td>&quot;S&quot;</td></tr><tr><td>5</td><td>0</td><td>3</td><td>&quot;Allen, Mr. Wil…</td><td>&quot;male&quot;</td><td>35.0</td><td>0</td><td>0</td><td>&quot;373450&quot;</td><td>8.05</td><td>null</td><td>&quot;S&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 12)\n",
       "┌─────────────┬──────────┬────────┬───────────────────┬───┬───────────┬─────────┬───────┬──────────┐\n",
       "│ PassengerId ┆ Survived ┆ Pclass ┆ Name              ┆ … ┆ Ticket    ┆ Fare    ┆ Cabin ┆ Embarked │\n",
       "│ ---         ┆ ---      ┆ ---    ┆ ---               ┆   ┆ ---       ┆ ---     ┆ ---   ┆ ---      │\n",
       "│ i64         ┆ i64      ┆ i64    ┆ str               ┆   ┆ str       ┆ f64     ┆ str   ┆ str      │\n",
       "╞═════════════╪══════════╪════════╪═══════════════════╪═══╪═══════════╪═════════╪═══════╪══════════╡\n",
       "│ 1           ┆ 0        ┆ 3      ┆ Braund, Mr. Owen  ┆ … ┆ A/5 21171 ┆ 7.25    ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Harris            ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 2           ┆ 1        ┆ 1      ┆ Cumings, Mrs.     ┆ … ┆ PC 17599  ┆ 71.2833 ┆ C85   ┆ C        │\n",
       "│             ┆          ┆        ┆ John Bradley      ┆   ┆           ┆         ┆       ┆          │\n",
       "│             ┆          ┆        ┆ (Flor…            ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 3           ┆ 1        ┆ 3      ┆ Heikkinen, Miss.  ┆ … ┆ STON/O2.  ┆ 7.925   ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Laina             ┆   ┆ 3101282   ┆         ┆       ┆          │\n",
       "│ 4           ┆ 1        ┆ 1      ┆ Futrelle, Mrs.    ┆ … ┆ 113803    ┆ 53.1    ┆ C123  ┆ S        │\n",
       "│             ┆          ┆        ┆ Jacques Heath     ┆   ┆           ┆         ┆       ┆          │\n",
       "│             ┆          ┆        ┆ (Li…              ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 5           ┆ 0        ┆ 3      ┆ Allen, Mr.        ┆ … ┆ 373450    ┆ 8.05    ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ William Henry     ┆   ┆           ┆         ┆       ┆          │\n",
       "└─────────────┴──────────┴────────┴───────────────────┴───┴───────────┴─────────┴───────┴──────────┘"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.with_columns(['Survived', 'Ticket', 'Fare']).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 12)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>PassengerId</th><th>Survived</th><th>Pclass</th><th>Name</th><th>Sex</th><th>Age</th><th>SibSp</th><th>Parch</th><th>Ticket</th><th>Fare</th><th>Cabin</th><th>Embarked</th></tr><tr><td>i64</td><td>i64</td><td>i64</td><td>str</td><td>str</td><td>f64</td><td>i64</td><td>i64</td><td>str</td><td>f64</td><td>str</td><td>str</td></tr></thead><tbody><tr><td>1</td><td>0</td><td>3</td><td>&quot;Braund, Mr. Ow…</td><td>&quot;male&quot;</td><td>22.0</td><td>1</td><td>0</td><td>&quot;A/5 21171&quot;</td><td>7.25</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>2</td><td>1</td><td>1</td><td>&quot;Cumings, Mrs. …</td><td>&quot;female&quot;</td><td>38.0</td><td>1</td><td>0</td><td>&quot;PC 17599&quot;</td><td>71.2833</td><td>&quot;C85&quot;</td><td>&quot;C&quot;</td></tr><tr><td>3</td><td>1</td><td>3</td><td>&quot;Heikkinen, Mis…</td><td>&quot;female&quot;</td><td>26.0</td><td>0</td><td>0</td><td>&quot;STON/O2. 31012…</td><td>7.925</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>4</td><td>1</td><td>1</td><td>&quot;Futrelle, Mrs.…</td><td>&quot;female&quot;</td><td>35.0</td><td>1</td><td>0</td><td>&quot;113803&quot;</td><td>53.1</td><td>&quot;C123&quot;</td><td>&quot;S&quot;</td></tr><tr><td>5</td><td>0</td><td>3</td><td>&quot;Allen, Mr. Wil…</td><td>&quot;male&quot;</td><td>35.0</td><td>0</td><td>0</td><td>&quot;373450&quot;</td><td>8.05</td><td>null</td><td>&quot;S&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 12)\n",
       "┌─────────────┬──────────┬────────┬───────────────────┬───┬───────────┬─────────┬───────┬──────────┐\n",
       "│ PassengerId ┆ Survived ┆ Pclass ┆ Name              ┆ … ┆ Ticket    ┆ Fare    ┆ Cabin ┆ Embarked │\n",
       "│ ---         ┆ ---      ┆ ---    ┆ ---               ┆   ┆ ---       ┆ ---     ┆ ---   ┆ ---      │\n",
       "│ i64         ┆ i64      ┆ i64    ┆ str               ┆   ┆ str       ┆ f64     ┆ str   ┆ str      │\n",
       "╞═════════════╪══════════╪════════╪═══════════════════╪═══╪═══════════╪═════════╪═══════╪══════════╡\n",
       "│ 1           ┆ 0        ┆ 3      ┆ Braund, Mr. Owen  ┆ … ┆ A/5 21171 ┆ 7.25    ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Harris            ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 2           ┆ 1        ┆ 1      ┆ Cumings, Mrs.     ┆ … ┆ PC 17599  ┆ 71.2833 ┆ C85   ┆ C        │\n",
       "│             ┆          ┆        ┆ John Bradley      ┆   ┆           ┆         ┆       ┆          │\n",
       "│             ┆          ┆        ┆ (Flor…            ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 3           ┆ 1        ┆ 3      ┆ Heikkinen, Miss.  ┆ … ┆ STON/O2.  ┆ 7.925   ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Laina             ┆   ┆ 3101282   ┆         ┆       ┆          │\n",
       "│ 4           ┆ 1        ┆ 1      ┆ Futrelle, Mrs.    ┆ … ┆ 113803    ┆ 53.1    ┆ C123  ┆ S        │\n",
       "│             ┆          ┆        ┆ Jacques Heath     ┆   ┆           ┆         ┆       ┆          │\n",
       "│             ┆          ┆        ┆ (Li…              ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 5           ┆ 0        ┆ 3      ┆ Allen, Mr.        ┆ … ┆ 373450    ┆ 8.05    ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ William Henry     ┆   ┆           ┆         ┆       ┆          │\n",
       "└─────────────┴──────────┴────────┴───────────────────┴───┴───────────┴─────────┴───────┴──────────┘"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.with_columns(\n",
    "    pl.col('Survived'),\n",
    "    pl.col('Ticket'),\n",
    "    pl.col('Fare')\n",
    ").head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 12)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>PassengerId</th><th>Survived</th><th>Pclass</th><th>Name</th><th>Sex</th><th>Age</th><th>SibSp</th><th>Parch</th><th>Ticket</th><th>Fare</th><th>Cabin</th><th>Embarked</th></tr><tr><td>i64</td><td>i64</td><td>i64</td><td>str</td><td>str</td><td>f64</td><td>i64</td><td>i64</td><td>str</td><td>f64</td><td>str</td><td>str</td></tr></thead><tbody><tr><td>2</td><td>1</td><td>1</td><td>&quot;Cumings, Mrs. …</td><td>&quot;female&quot;</td><td>38.0</td><td>1</td><td>0</td><td>&quot;PC 17599&quot;</td><td>71.2833</td><td>&quot;C85&quot;</td><td>&quot;C&quot;</td></tr><tr><td>4</td><td>1</td><td>1</td><td>&quot;Futrelle, Mrs.…</td><td>&quot;female&quot;</td><td>35.0</td><td>1</td><td>0</td><td>&quot;113803&quot;</td><td>53.1</td><td>&quot;C123&quot;</td><td>&quot;S&quot;</td></tr><tr><td>5</td><td>0</td><td>3</td><td>&quot;Allen, Mr. Wil…</td><td>&quot;male&quot;</td><td>35.0</td><td>0</td><td>0</td><td>&quot;373450&quot;</td><td>8.05</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>7</td><td>0</td><td>1</td><td>&quot;McCarthy, Mr. …</td><td>&quot;male&quot;</td><td>54.0</td><td>0</td><td>0</td><td>&quot;17463&quot;</td><td>51.8625</td><td>&quot;E46&quot;</td><td>&quot;S&quot;</td></tr><tr><td>12</td><td>1</td><td>1</td><td>&quot;Bonnell, Miss.…</td><td>&quot;female&quot;</td><td>58.0</td><td>0</td><td>0</td><td>&quot;113783&quot;</td><td>26.55</td><td>&quot;C103&quot;</td><td>&quot;S&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 12)\n",
       "┌─────────────┬──────────┬────────┬────────────────────┬───┬──────────┬─────────┬───────┬──────────┐\n",
       "│ PassengerId ┆ Survived ┆ Pclass ┆ Name               ┆ … ┆ Ticket   ┆ Fare    ┆ Cabin ┆ Embarked │\n",
       "│ ---         ┆ ---      ┆ ---    ┆ ---                ┆   ┆ ---      ┆ ---     ┆ ---   ┆ ---      │\n",
       "│ i64         ┆ i64      ┆ i64    ┆ str                ┆   ┆ str      ┆ f64     ┆ str   ┆ str      │\n",
       "╞═════════════╪══════════╪════════╪════════════════════╪═══╪══════════╪═════════╪═══════╪══════════╡\n",
       "│ 2           ┆ 1        ┆ 1      ┆ Cumings, Mrs. John ┆ … ┆ PC 17599 ┆ 71.2833 ┆ C85   ┆ C        │\n",
       "│             ┆          ┆        ┆ Bradley (Flor…     ┆   ┆          ┆         ┆       ┆          │\n",
       "│ 4           ┆ 1        ┆ 1      ┆ Futrelle, Mrs.     ┆ … ┆ 113803   ┆ 53.1    ┆ C123  ┆ S        │\n",
       "│             ┆          ┆        ┆ Jacques Heath (Li… ┆   ┆          ┆         ┆       ┆          │\n",
       "│ 5           ┆ 0        ┆ 3      ┆ Allen, Mr. William ┆ … ┆ 373450   ┆ 8.05    ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Henry              ┆   ┆          ┆         ┆       ┆          │\n",
       "│ 7           ┆ 0        ┆ 1      ┆ McCarthy, Mr.      ┆ … ┆ 17463    ┆ 51.8625 ┆ E46   ┆ S        │\n",
       "│             ┆          ┆        ┆ Timothy J          ┆   ┆          ┆         ┆       ┆          │\n",
       "│ 12          ┆ 1        ┆ 1      ┆ Bonnell, Miss.     ┆ … ┆ 113783   ┆ 26.55   ┆ C103  ┆ S        │\n",
       "│             ┆          ┆        ┆ Elizabeth          ┆   ┆          ┆         ┆       ┆          │\n",
       "└─────────────┴──────────┴────────┴────────────────────┴───┴──────────┴─────────┴───────┴──────────┘"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.filter((pl.col('Age') >= 30)).head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 12)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>PassengerId</th><th>Survived</th><th>Pclass</th><th>Name</th><th>Sex</th><th>Age</th><th>SibSp</th><th>Parch</th><th>Ticket</th><th>Fare</th><th>Cabin</th><th>Embarked</th></tr><tr><td>i64</td><td>i64</td><td>i64</td><td>str</td><td>str</td><td>f64</td><td>i64</td><td>i64</td><td>str</td><td>f64</td><td>str</td><td>str</td></tr></thead><tbody><tr><td>5</td><td>0</td><td>3</td><td>&quot;Allen, Mr. Wil…</td><td>&quot;male&quot;</td><td>35.0</td><td>0</td><td>0</td><td>&quot;373450&quot;</td><td>8.05</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>7</td><td>0</td><td>1</td><td>&quot;McCarthy, Mr. …</td><td>&quot;male&quot;</td><td>54.0</td><td>0</td><td>0</td><td>&quot;17463&quot;</td><td>51.8625</td><td>&quot;E46&quot;</td><td>&quot;S&quot;</td></tr><tr><td>14</td><td>0</td><td>3</td><td>&quot;Andersson, Mr.…</td><td>&quot;male&quot;</td><td>39.0</td><td>1</td><td>5</td><td>&quot;347082&quot;</td><td>31.275</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>21</td><td>0</td><td>2</td><td>&quot;Fynney, Mr. Jo…</td><td>&quot;male&quot;</td><td>35.0</td><td>0</td><td>0</td><td>&quot;239865&quot;</td><td>26.0</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>22</td><td>1</td><td>2</td><td>&quot;Beesley, Mr. L…</td><td>&quot;male&quot;</td><td>34.0</td><td>0</td><td>0</td><td>&quot;248698&quot;</td><td>13.0</td><td>&quot;D56&quot;</td><td>&quot;S&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 12)\n",
       "┌─────────────┬──────────┬────────┬──────────────────────┬───┬────────┬─────────┬───────┬──────────┐\n",
       "│ PassengerId ┆ Survived ┆ Pclass ┆ Name                 ┆ … ┆ Ticket ┆ Fare    ┆ Cabin ┆ Embarked │\n",
       "│ ---         ┆ ---      ┆ ---    ┆ ---                  ┆   ┆ ---    ┆ ---     ┆ ---   ┆ ---      │\n",
       "│ i64         ┆ i64      ┆ i64    ┆ str                  ┆   ┆ str    ┆ f64     ┆ str   ┆ str      │\n",
       "╞═════════════╪══════════╪════════╪══════════════════════╪═══╪════════╪═════════╪═══════╪══════════╡\n",
       "│ 5           ┆ 0        ┆ 3      ┆ Allen, Mr. William   ┆ … ┆ 373450 ┆ 8.05    ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Henry                ┆   ┆        ┆         ┆       ┆          │\n",
       "│ 7           ┆ 0        ┆ 1      ┆ McCarthy, Mr.        ┆ … ┆ 17463  ┆ 51.8625 ┆ E46   ┆ S        │\n",
       "│             ┆          ┆        ┆ Timothy J            ┆   ┆        ┆         ┆       ┆          │\n",
       "│ 14          ┆ 0        ┆ 3      ┆ Andersson, Mr.       ┆ … ┆ 347082 ┆ 31.275  ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Anders Johan         ┆   ┆        ┆         ┆       ┆          │\n",
       "│ 21          ┆ 0        ┆ 2      ┆ Fynney, Mr. Joseph J ┆ … ┆ 239865 ┆ 26.0    ┆ null  ┆ S        │\n",
       "│ 22          ┆ 1        ┆ 2      ┆ Beesley, Mr.         ┆ … ┆ 248698 ┆ 13.0    ┆ D56   ┆ S        │\n",
       "│             ┆          ┆        ┆ Lawrence             ┆   ┆        ┆         ┆       ┆          │\n",
       "└─────────────┴──────────┴────────┴──────────────────────┴───┴────────┴─────────┴───────┴──────────┘"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.filter(\n",
    "    (pl.col('Age') >= 30) & (pl.col('Sex')=='male')\n",
    ").head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There is more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 2)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Age</th><th>Sex</th></tr><tr><td>f64</td><td>str</td></tr></thead><tbody><tr><td>22.0</td><td>&quot;male&quot;</td></tr><tr><td>38.0</td><td>&quot;female&quot;</td></tr><tr><td>26.0</td><td>&quot;female&quot;</td></tr><tr><td>35.0</td><td>&quot;female&quot;</td></tr><tr><td>35.0</td><td>&quot;male&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 2)\n",
       "┌──────┬────────┐\n",
       "│ Age  ┆ Sex    │\n",
       "│ ---  ┆ ---    │\n",
       "│ f64  ┆ str    │\n",
       "╞══════╪════════╡\n",
       "│ 22.0 ┆ male   │\n",
       "│ 38.0 ┆ female │\n",
       "│ 26.0 ┆ female │\n",
       "│ 35.0 ┆ female │\n",
       "│ 35.0 ┆ male   │\n",
       "└──────┴────────┘"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[['Age', 'Sex']].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 4)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Name</th><th>Sex</th><th>Age</th><th>Fare</th></tr><tr><td>str</td><td>str</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>&quot;Braund, Mr. Ow…</td><td>&quot;male&quot;</td><td>22.0</td><td>7.25</td></tr><tr><td>&quot;Cumings, Mrs. …</td><td>&quot;female&quot;</td><td>38.0</td><td>71.2833</td></tr><tr><td>&quot;Heikkinen, Mis…</td><td>&quot;female&quot;</td><td>26.0</td><td>7.925</td></tr><tr><td>&quot;Futrelle, Mrs.…</td><td>&quot;female&quot;</td><td>35.0</td><td>53.1</td></tr><tr><td>&quot;Allen, Mr. Wil…</td><td>&quot;male&quot;</td><td>35.0</td><td>8.05</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 4)\n",
       "┌───────────────────────────────────┬────────┬──────┬─────────┐\n",
       "│ Name                              ┆ Sex    ┆ Age  ┆ Fare    │\n",
       "│ ---                               ┆ ---    ┆ ---  ┆ ---     │\n",
       "│ str                               ┆ str    ┆ f64  ┆ f64     │\n",
       "╞═══════════════════════════════════╪════════╪══════╪═════════╡\n",
       "│ Braund, Mr. Owen Harris           ┆ male   ┆ 22.0 ┆ 7.25    │\n",
       "│ Cumings, Mrs. John Bradley (Flor… ┆ female ┆ 38.0 ┆ 71.2833 │\n",
       "│ Heikkinen, Miss. Laina            ┆ female ┆ 26.0 ┆ 7.925   │\n",
       "│ Futrelle, Mrs. Jacques Heath (Li… ┆ female ┆ 35.0 ┆ 53.1    │\n",
       "│ Allen, Mr. William Henry          ┆ male   ┆ 35.0 ┆ 8.05    │\n",
       "└───────────────────────────────────┴────────┴──────┴─────────┘"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.select(pl.col('^[a-zA-Z]{0,4}$')).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 5)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Name</th><th>Sex</th><th>Ticket</th><th>Cabin</th><th>Embarked</th></tr><tr><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td></tr></thead><tbody><tr><td>&quot;Braund, Mr. Ow…</td><td>&quot;male&quot;</td><td>&quot;A/5 21171&quot;</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>&quot;Cumings, Mrs. …</td><td>&quot;female&quot;</td><td>&quot;PC 17599&quot;</td><td>&quot;C85&quot;</td><td>&quot;C&quot;</td></tr><tr><td>&quot;Heikkinen, Mis…</td><td>&quot;female&quot;</td><td>&quot;STON/O2. 31012…</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>&quot;Futrelle, Mrs.…</td><td>&quot;female&quot;</td><td>&quot;113803&quot;</td><td>&quot;C123&quot;</td><td>&quot;S&quot;</td></tr><tr><td>&quot;Allen, Mr. Wil…</td><td>&quot;male&quot;</td><td>&quot;373450&quot;</td><td>null</td><td>&quot;S&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 5)\n",
       "┌───────────────────────────────────┬────────┬──────────────────┬───────┬──────────┐\n",
       "│ Name                              ┆ Sex    ┆ Ticket           ┆ Cabin ┆ Embarked │\n",
       "│ ---                               ┆ ---    ┆ ---              ┆ ---   ┆ ---      │\n",
       "│ str                               ┆ str    ┆ str              ┆ str   ┆ str      │\n",
       "╞═══════════════════════════════════╪════════╪══════════════════╪═══════╪══════════╡\n",
       "│ Braund, Mr. Owen Harris           ┆ male   ┆ A/5 21171        ┆ null  ┆ S        │\n",
       "│ Cumings, Mrs. John Bradley (Flor… ┆ female ┆ PC 17599         ┆ C85   ┆ C        │\n",
       "│ Heikkinen, Miss. Laina            ┆ female ┆ STON/O2. 3101282 ┆ null  ┆ S        │\n",
       "│ Futrelle, Mrs. Jacques Heath (Li… ┆ female ┆ 113803           ┆ C123  ┆ S        │\n",
       "│ Allen, Mr. William Henry          ┆ male   ┆ 373450           ┆ null  ┆ S        │\n",
       "└───────────────────────────────────┴────────┴──────────────────┴───────┴──────────┘"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.select(pl.col(pl.String)).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 7)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>PassengerId</th><th>Survived</th><th>Pclass</th><th>Age</th><th>SibSp</th><th>Parch</th><th>Fare</th></tr><tr><td>i64</td><td>i64</td><td>i64</td><td>f64</td><td>i64</td><td>i64</td><td>f64</td></tr></thead><tbody><tr><td>1</td><td>0</td><td>3</td><td>22.0</td><td>1</td><td>0</td><td>7.25</td></tr><tr><td>2</td><td>1</td><td>1</td><td>38.0</td><td>1</td><td>0</td><td>71.2833</td></tr><tr><td>3</td><td>1</td><td>3</td><td>26.0</td><td>0</td><td>0</td><td>7.925</td></tr><tr><td>4</td><td>1</td><td>1</td><td>35.0</td><td>1</td><td>0</td><td>53.1</td></tr><tr><td>5</td><td>0</td><td>3</td><td>35.0</td><td>0</td><td>0</td><td>8.05</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 7)\n",
       "┌─────────────┬──────────┬────────┬──────┬───────┬───────┬─────────┐\n",
       "│ PassengerId ┆ Survived ┆ Pclass ┆ Age  ┆ SibSp ┆ Parch ┆ Fare    │\n",
       "│ ---         ┆ ---      ┆ ---    ┆ ---  ┆ ---   ┆ ---   ┆ ---     │\n",
       "│ i64         ┆ i64      ┆ i64    ┆ f64  ┆ i64   ┆ i64   ┆ f64     │\n",
       "╞═════════════╪══════════╪════════╪══════╪═══════╪═══════╪═════════╡\n",
       "│ 1           ┆ 0        ┆ 3      ┆ 22.0 ┆ 1     ┆ 0     ┆ 7.25    │\n",
       "│ 2           ┆ 1        ┆ 1      ┆ 38.0 ┆ 1     ┆ 0     ┆ 71.2833 │\n",
       "│ 3           ┆ 1        ┆ 3      ┆ 26.0 ┆ 0     ┆ 0     ┆ 7.925   │\n",
       "│ 4           ┆ 1        ┆ 1      ┆ 35.0 ┆ 1     ┆ 0     ┆ 53.1    │\n",
       "│ 5           ┆ 0        ┆ 3      ┆ 35.0 ┆ 0     ┆ 0     ┆ 8.05    │\n",
       "└─────────────┴──────────┴────────┴──────┴───────┴───────┴─────────┘"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import polars.selectors as cs\n",
    "\n",
    "df.select(cs.numeric()).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 3)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>PassengerId</th><th>Survived</th><th>Embarked</th></tr><tr><td>i64</td><td>i64</td><td>str</td></tr></thead><tbody><tr><td>1</td><td>0</td><td>&quot;S&quot;</td></tr><tr><td>2</td><td>1</td><td>&quot;C&quot;</td></tr><tr><td>3</td><td>1</td><td>&quot;S&quot;</td></tr><tr><td>4</td><td>1</td><td>&quot;S&quot;</td></tr><tr><td>5</td><td>0</td><td>&quot;S&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 3)\n",
       "┌─────────────┬──────────┬──────────┐\n",
       "│ PassengerId ┆ Survived ┆ Embarked │\n",
       "│ ---         ┆ ---      ┆ ---      │\n",
       "│ i64         ┆ i64      ┆ str      │\n",
       "╞═════════════╪══════════╪══════════╡\n",
       "│ 1           ┆ 0        ┆ S        │\n",
       "│ 2           ┆ 1        ┆ C        │\n",
       "│ 3           ┆ 1        ┆ S        │\n",
       "│ 4           ┆ 1        ┆ S        │\n",
       "│ 5           ┆ 0        ┆ S        │\n",
       "└─────────────┴──────────┴──────────┘"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.select(cs.matches('se|ed')).head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Creating, modifying, and deleting columns "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 12)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>PassengerId</th><th>Survived</th><th>Pclass</th><th>Name</th><th>Sex</th><th>Age</th><th>SibSp</th><th>Parch</th><th>Ticket</th><th>Fare</th><th>Cabin</th><th>Embarked</th></tr><tr><td>i64</td><td>i64</td><td>i64</td><td>str</td><td>str</td><td>f64</td><td>i64</td><td>i64</td><td>str</td><td>f64</td><td>str</td><td>str</td></tr></thead><tbody><tr><td>1</td><td>0</td><td>3</td><td>&quot;Braund, Mr. Ow…</td><td>&quot;male&quot;</td><td>22.0</td><td>1</td><td>0</td><td>&quot;A/5 21171&quot;</td><td>7.25</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>2</td><td>1</td><td>1</td><td>&quot;Cumings, Mrs. …</td><td>&quot;female&quot;</td><td>38.0</td><td>1</td><td>0</td><td>&quot;PC 17599&quot;</td><td>71.2833</td><td>&quot;C85&quot;</td><td>&quot;C&quot;</td></tr><tr><td>3</td><td>1</td><td>3</td><td>&quot;Heikkinen, Mis…</td><td>&quot;female&quot;</td><td>26.0</td><td>0</td><td>0</td><td>&quot;STON/O2. 31012…</td><td>7.925</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>4</td><td>1</td><td>1</td><td>&quot;Futrelle, Mrs.…</td><td>&quot;female&quot;</td><td>35.0</td><td>1</td><td>0</td><td>&quot;113803&quot;</td><td>53.1</td><td>&quot;C123&quot;</td><td>&quot;S&quot;</td></tr><tr><td>5</td><td>0</td><td>3</td><td>&quot;Allen, Mr. Wil…</td><td>&quot;male&quot;</td><td>35.0</td><td>0</td><td>0</td><td>&quot;373450&quot;</td><td>8.05</td><td>null</td><td>&quot;S&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 12)\n",
       "┌─────────────┬──────────┬────────┬───────────────────┬───┬───────────┬─────────┬───────┬──────────┐\n",
       "│ PassengerId ┆ Survived ┆ Pclass ┆ Name              ┆ … ┆ Ticket    ┆ Fare    ┆ Cabin ┆ Embarked │\n",
       "│ ---         ┆ ---      ┆ ---    ┆ ---               ┆   ┆ ---       ┆ ---     ┆ ---   ┆ ---      │\n",
       "│ i64         ┆ i64      ┆ i64    ┆ str               ┆   ┆ str       ┆ f64     ┆ str   ┆ str      │\n",
       "╞═════════════╪══════════╪════════╪═══════════════════╪═══╪═══════════╪═════════╪═══════╪══════════╡\n",
       "│ 1           ┆ 0        ┆ 3      ┆ Braund, Mr. Owen  ┆ … ┆ A/5 21171 ┆ 7.25    ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Harris            ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 2           ┆ 1        ┆ 1      ┆ Cumings, Mrs.     ┆ … ┆ PC 17599  ┆ 71.2833 ┆ C85   ┆ C        │\n",
       "│             ┆          ┆        ┆ John Bradley      ┆   ┆           ┆         ┆       ┆          │\n",
       "│             ┆          ┆        ┆ (Flor…            ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 3           ┆ 1        ┆ 3      ┆ Heikkinen, Miss.  ┆ … ┆ STON/O2.  ┆ 7.925   ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Laina             ┆   ┆ 3101282   ┆         ┆       ┆          │\n",
       "│ 4           ┆ 1        ┆ 1      ┆ Futrelle, Mrs.    ┆ … ┆ 113803    ┆ 53.1    ┆ C123  ┆ S        │\n",
       "│             ┆          ┆        ┆ Jacques Heath     ┆   ┆           ┆         ┆       ┆          │\n",
       "│             ┆          ┆        ┆ (Li…              ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 5           ┆ 0        ┆ 3      ┆ Allen, Mr.        ┆ … ┆ 373450    ┆ 8.05    ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ William Henry     ┆   ┆           ┆         ┆       ┆          │\n",
       "└─────────────┴──────────┴────────┴───────────────────┴───┴───────────┴─────────┴───────┴──────────┘"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pl.read_csv('../data/titanic_dataset.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 13)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>PassengerId</th><th>Survived</th><th>Pclass</th><th>Name</th><th>Sex</th><th>Age</th><th>SibSp</th><th>Parch</th><th>Ticket</th><th>Fare</th><th>Cabin</th><th>Embarked</th><th>Max Fare</th></tr><tr><td>i64</td><td>i64</td><td>i64</td><td>str</td><td>str</td><td>f64</td><td>i64</td><td>i64</td><td>str</td><td>f64</td><td>str</td><td>str</td><td>f64</td></tr></thead><tbody><tr><td>1</td><td>0</td><td>3</td><td>&quot;Braund, Mr. Ow…</td><td>&quot;male&quot;</td><td>22.0</td><td>1</td><td>0</td><td>&quot;A/5 21171&quot;</td><td>7.25</td><td>null</td><td>&quot;S&quot;</td><td>512.3292</td></tr><tr><td>2</td><td>1</td><td>1</td><td>&quot;Cumings, Mrs. …</td><td>&quot;female&quot;</td><td>38.0</td><td>1</td><td>0</td><td>&quot;PC 17599&quot;</td><td>71.2833</td><td>&quot;C85&quot;</td><td>&quot;C&quot;</td><td>512.3292</td></tr><tr><td>3</td><td>1</td><td>3</td><td>&quot;Heikkinen, Mis…</td><td>&quot;female&quot;</td><td>26.0</td><td>0</td><td>0</td><td>&quot;STON/O2. 31012…</td><td>7.925</td><td>null</td><td>&quot;S&quot;</td><td>512.3292</td></tr><tr><td>4</td><td>1</td><td>1</td><td>&quot;Futrelle, Mrs.…</td><td>&quot;female&quot;</td><td>35.0</td><td>1</td><td>0</td><td>&quot;113803&quot;</td><td>53.1</td><td>&quot;C123&quot;</td><td>&quot;S&quot;</td><td>512.3292</td></tr><tr><td>5</td><td>0</td><td>3</td><td>&quot;Allen, Mr. Wil…</td><td>&quot;male&quot;</td><td>35.0</td><td>0</td><td>0</td><td>&quot;373450&quot;</td><td>8.05</td><td>null</td><td>&quot;S&quot;</td><td>512.3292</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 13)\n",
       "┌─────────────┬──────────┬────────┬────────────────────┬───┬─────────┬───────┬──────────┬──────────┐\n",
       "│ PassengerId ┆ Survived ┆ Pclass ┆ Name               ┆ … ┆ Fare    ┆ Cabin ┆ Embarked ┆ Max Fare │\n",
       "│ ---         ┆ ---      ┆ ---    ┆ ---                ┆   ┆ ---     ┆ ---   ┆ ---      ┆ ---      │\n",
       "│ i64         ┆ i64      ┆ i64    ┆ str                ┆   ┆ f64     ┆ str   ┆ str      ┆ f64      │\n",
       "╞═════════════╪══════════╪════════╪════════════════════╪═══╪═════════╪═══════╪══════════╪══════════╡\n",
       "│ 1           ┆ 0        ┆ 3      ┆ Braund, Mr. Owen   ┆ … ┆ 7.25    ┆ null  ┆ S        ┆ 512.3292 │\n",
       "│             ┆          ┆        ┆ Harris             ┆   ┆         ┆       ┆          ┆          │\n",
       "│ 2           ┆ 1        ┆ 1      ┆ Cumings, Mrs. John ┆ … ┆ 71.2833 ┆ C85   ┆ C        ┆ 512.3292 │\n",
       "│             ┆          ┆        ┆ Bradley (Flor…     ┆   ┆         ┆       ┆          ┆          │\n",
       "│ 3           ┆ 1        ┆ 3      ┆ Heikkinen, Miss.   ┆ … ┆ 7.925   ┆ null  ┆ S        ┆ 512.3292 │\n",
       "│             ┆          ┆        ┆ Laina              ┆   ┆         ┆       ┆          ┆          │\n",
       "│ 4           ┆ 1        ┆ 1      ┆ Futrelle, Mrs.     ┆ … ┆ 53.1    ┆ C123  ┆ S        ┆ 512.3292 │\n",
       "│             ┆          ┆        ┆ Jacques Heath (Li… ┆   ┆         ┆       ┆          ┆          │\n",
       "│ 5           ┆ 0        ┆ 3      ┆ Allen, Mr. William ┆ … ┆ 8.05    ┆ null  ┆ S        ┆ 512.3292 │\n",
       "│             ┆          ┆        ┆ Henry              ┆   ┆         ┆       ┆          ┆          │\n",
       "└─────────────┴──────────┴────────┴────────────────────┴───┴─────────┴───────┴──────────┴──────────┘"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.with_columns(\n",
    "    pl.col('Fare').max().alias('Max Fare')\n",
    ").head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 13)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>PassengerId</th><th>Survived</th><th>Pclass</th><th>Name</th><th>Sex</th><th>Age</th><th>SibSp</th><th>Parch</th><th>Ticket</th><th>Fare</th><th>Cabin</th><th>Embarked</th><th>max_fare</th></tr><tr><td>i64</td><td>i64</td><td>i64</td><td>str</td><td>str</td><td>f64</td><td>i64</td><td>i64</td><td>str</td><td>f64</td><td>str</td><td>str</td><td>f64</td></tr></thead><tbody><tr><td>1</td><td>0</td><td>3</td><td>&quot;Braund, Mr. Ow…</td><td>&quot;male&quot;</td><td>22.0</td><td>1</td><td>0</td><td>&quot;A/5 21171&quot;</td><td>7.25</td><td>null</td><td>&quot;S&quot;</td><td>512.3292</td></tr><tr><td>2</td><td>1</td><td>1</td><td>&quot;Cumings, Mrs. …</td><td>&quot;female&quot;</td><td>38.0</td><td>1</td><td>0</td><td>&quot;PC 17599&quot;</td><td>71.2833</td><td>&quot;C85&quot;</td><td>&quot;C&quot;</td><td>512.3292</td></tr><tr><td>3</td><td>1</td><td>3</td><td>&quot;Heikkinen, Mis…</td><td>&quot;female&quot;</td><td>26.0</td><td>0</td><td>0</td><td>&quot;STON/O2. 31012…</td><td>7.925</td><td>null</td><td>&quot;S&quot;</td><td>512.3292</td></tr><tr><td>4</td><td>1</td><td>1</td><td>&quot;Futrelle, Mrs.…</td><td>&quot;female&quot;</td><td>35.0</td><td>1</td><td>0</td><td>&quot;113803&quot;</td><td>53.1</td><td>&quot;C123&quot;</td><td>&quot;S&quot;</td><td>512.3292</td></tr><tr><td>5</td><td>0</td><td>3</td><td>&quot;Allen, Mr. Wil…</td><td>&quot;male&quot;</td><td>35.0</td><td>0</td><td>0</td><td>&quot;373450&quot;</td><td>8.05</td><td>null</td><td>&quot;S&quot;</td><td>512.3292</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 13)\n",
       "┌─────────────┬──────────┬────────┬────────────────────┬───┬─────────┬───────┬──────────┬──────────┐\n",
       "│ PassengerId ┆ Survived ┆ Pclass ┆ Name               ┆ … ┆ Fare    ┆ Cabin ┆ Embarked ┆ max_fare │\n",
       "│ ---         ┆ ---      ┆ ---    ┆ ---                ┆   ┆ ---     ┆ ---   ┆ ---      ┆ ---      │\n",
       "│ i64         ┆ i64      ┆ i64    ┆ str                ┆   ┆ f64     ┆ str   ┆ str      ┆ f64      │\n",
       "╞═════════════╪══════════╪════════╪════════════════════╪═══╪═════════╪═══════╪══════════╪══════════╡\n",
       "│ 1           ┆ 0        ┆ 3      ┆ Braund, Mr. Owen   ┆ … ┆ 7.25    ┆ null  ┆ S        ┆ 512.3292 │\n",
       "│             ┆          ┆        ┆ Harris             ┆   ┆         ┆       ┆          ┆          │\n",
       "│ 2           ┆ 1        ┆ 1      ┆ Cumings, Mrs. John ┆ … ┆ 71.2833 ┆ C85   ┆ C        ┆ 512.3292 │\n",
       "│             ┆          ┆        ┆ Bradley (Flor…     ┆   ┆         ┆       ┆          ┆          │\n",
       "│ 3           ┆ 1        ┆ 3      ┆ Heikkinen, Miss.   ┆ … ┆ 7.925   ┆ null  ┆ S        ┆ 512.3292 │\n",
       "│             ┆          ┆        ┆ Laina              ┆   ┆         ┆       ┆          ┆          │\n",
       "│ 4           ┆ 1        ┆ 1      ┆ Futrelle, Mrs.     ┆ … ┆ 53.1    ┆ C123  ┆ S        ┆ 512.3292 │\n",
       "│             ┆          ┆        ┆ Jacques Heath (Li… ┆   ┆         ┆       ┆          ┆          │\n",
       "│ 5           ┆ 0        ┆ 3      ┆ Allen, Mr. William ┆ … ┆ 8.05    ┆ null  ┆ S        ┆ 512.3292 │\n",
       "│             ┆          ┆        ┆ Henry              ┆   ┆         ┆       ┆          ┆          │\n",
       "└─────────────┴──────────┴────────┴────────────────────┴───┴─────────┴───────┴──────────┴──────────┘"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.with_columns(\n",
    "    max_fare=pl.col('Fare').max()\n",
    ").head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 13)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>PassengerId</th><th>Survived</th><th>Pclass</th><th>Name</th><th>Sex</th><th>Age</th><th>SibSp</th><th>Parch</th><th>Ticket</th><th>Fare</th><th>Cabin</th><th>Embarked</th><th>Max Fare - Avg Fare</th></tr><tr><td>i64</td><td>i64</td><td>i64</td><td>str</td><td>str</td><td>f64</td><td>i64</td><td>i64</td><td>str</td><td>f64</td><td>str</td><td>str</td><td>f64</td></tr></thead><tbody><tr><td>1</td><td>0</td><td>3</td><td>&quot;Braund, Mr. Ow…</td><td>&quot;male&quot;</td><td>22.0</td><td>1</td><td>0</td><td>&quot;A/5 21171&quot;</td><td>7.25</td><td>null</td><td>&quot;S&quot;</td><td>480.124992</td></tr><tr><td>2</td><td>1</td><td>1</td><td>&quot;Cumings, Mrs. …</td><td>&quot;female&quot;</td><td>38.0</td><td>1</td><td>0</td><td>&quot;PC 17599&quot;</td><td>71.2833</td><td>&quot;C85&quot;</td><td>&quot;C&quot;</td><td>480.124992</td></tr><tr><td>3</td><td>1</td><td>3</td><td>&quot;Heikkinen, Mis…</td><td>&quot;female&quot;</td><td>26.0</td><td>0</td><td>0</td><td>&quot;STON/O2. 31012…</td><td>7.925</td><td>null</td><td>&quot;S&quot;</td><td>480.124992</td></tr><tr><td>4</td><td>1</td><td>1</td><td>&quot;Futrelle, Mrs.…</td><td>&quot;female&quot;</td><td>35.0</td><td>1</td><td>0</td><td>&quot;113803&quot;</td><td>53.1</td><td>&quot;C123&quot;</td><td>&quot;S&quot;</td><td>480.124992</td></tr><tr><td>5</td><td>0</td><td>3</td><td>&quot;Allen, Mr. Wil…</td><td>&quot;male&quot;</td><td>35.0</td><td>0</td><td>0</td><td>&quot;373450&quot;</td><td>8.05</td><td>null</td><td>&quot;S&quot;</td><td>480.124992</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 13)\n",
       "┌─────────────┬──────────┬────────┬──────────────────┬───┬─────────┬───────┬──────────┬────────────┐\n",
       "│ PassengerId ┆ Survived ┆ Pclass ┆ Name             ┆ … ┆ Fare    ┆ Cabin ┆ Embarked ┆ Max Fare - │\n",
       "│ ---         ┆ ---      ┆ ---    ┆ ---              ┆   ┆ ---     ┆ ---   ┆ ---      ┆ Avg Fare   │\n",
       "│ i64         ┆ i64      ┆ i64    ┆ str              ┆   ┆ f64     ┆ str   ┆ str      ┆ ---        │\n",
       "│             ┆          ┆        ┆                  ┆   ┆         ┆       ┆          ┆ f64        │\n",
       "╞═════════════╪══════════╪════════╪══════════════════╪═══╪═════════╪═══════╪══════════╪════════════╡\n",
       "│ 1           ┆ 0        ┆ 3      ┆ Braund, Mr. Owen ┆ … ┆ 7.25    ┆ null  ┆ S        ┆ 480.124992 │\n",
       "│             ┆          ┆        ┆ Harris           ┆   ┆         ┆       ┆          ┆            │\n",
       "│ 2           ┆ 1        ┆ 1      ┆ Cumings, Mrs.    ┆ … ┆ 71.2833 ┆ C85   ┆ C        ┆ 480.124992 │\n",
       "│             ┆          ┆        ┆ John Bradley     ┆   ┆         ┆       ┆          ┆            │\n",
       "│             ┆          ┆        ┆ (Flor…           ┆   ┆         ┆       ┆          ┆            │\n",
       "│ 3           ┆ 1        ┆ 3      ┆ Heikkinen, Miss. ┆ … ┆ 7.925   ┆ null  ┆ S        ┆ 480.124992 │\n",
       "│             ┆          ┆        ┆ Laina            ┆   ┆         ┆       ┆          ┆            │\n",
       "│ 4           ┆ 1        ┆ 1      ┆ Futrelle, Mrs.   ┆ … ┆ 53.1    ┆ C123  ┆ S        ┆ 480.124992 │\n",
       "│             ┆          ┆        ┆ Jacques Heath    ┆   ┆         ┆       ┆          ┆            │\n",
       "│             ┆          ┆        ┆ (Li…             ┆   ┆         ┆       ┆          ┆            │\n",
       "│ 5           ┆ 0        ┆ 3      ┆ Allen, Mr.       ┆ … ┆ 8.05    ┆ null  ┆ S        ┆ 480.124992 │\n",
       "│             ┆          ┆        ┆ William Henry    ┆   ┆         ┆       ┆          ┆            │\n",
       "└─────────────┴──────────┴────────┴──────────────────┴───┴─────────┴───────┴──────────┴────────────┘"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.with_columns(\n",
    "    (pl.col('Fare').max() - pl.col('Fare').mean()).alias('Max Fare - Avg Fare')\n",
    ").head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 13)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>PassengerId</th><th>Survived</th><th>Pclass</th><th>Name</th><th>Sex</th><th>Age</th><th>SibSp</th><th>Parch</th><th>Ticket</th><th>Fare</th><th>Cabin</th><th>Embarked</th><th>literal</th></tr><tr><td>i64</td><td>i64</td><td>i64</td><td>str</td><td>str</td><td>f64</td><td>i64</td><td>i64</td><td>str</td><td>f64</td><td>str</td><td>str</td><td>str</td></tr></thead><tbody><tr><td>1</td><td>0</td><td>3</td><td>&quot;Braund, Mr. Ow…</td><td>&quot;male&quot;</td><td>22.0</td><td>1</td><td>0</td><td>&quot;A/5 21171&quot;</td><td>7.25</td><td>null</td><td>&quot;S&quot;</td><td>&quot;Titanic&quot;</td></tr><tr><td>2</td><td>1</td><td>1</td><td>&quot;Cumings, Mrs. …</td><td>&quot;female&quot;</td><td>38.0</td><td>1</td><td>0</td><td>&quot;PC 17599&quot;</td><td>71.2833</td><td>&quot;C85&quot;</td><td>&quot;C&quot;</td><td>&quot;Titanic&quot;</td></tr><tr><td>3</td><td>1</td><td>3</td><td>&quot;Heikkinen, Mis…</td><td>&quot;female&quot;</td><td>26.0</td><td>0</td><td>0</td><td>&quot;STON/O2. 31012…</td><td>7.925</td><td>null</td><td>&quot;S&quot;</td><td>&quot;Titanic&quot;</td></tr><tr><td>4</td><td>1</td><td>1</td><td>&quot;Futrelle, Mrs.…</td><td>&quot;female&quot;</td><td>35.0</td><td>1</td><td>0</td><td>&quot;113803&quot;</td><td>53.1</td><td>&quot;C123&quot;</td><td>&quot;S&quot;</td><td>&quot;Titanic&quot;</td></tr><tr><td>5</td><td>0</td><td>3</td><td>&quot;Allen, Mr. Wil…</td><td>&quot;male&quot;</td><td>35.0</td><td>0</td><td>0</td><td>&quot;373450&quot;</td><td>8.05</td><td>null</td><td>&quot;S&quot;</td><td>&quot;Titanic&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 13)\n",
       "┌─────────────┬──────────┬────────┬─────────────────────┬───┬─────────┬───────┬──────────┬─────────┐\n",
       "│ PassengerId ┆ Survived ┆ Pclass ┆ Name                ┆ … ┆ Fare    ┆ Cabin ┆ Embarked ┆ literal │\n",
       "│ ---         ┆ ---      ┆ ---    ┆ ---                 ┆   ┆ ---     ┆ ---   ┆ ---      ┆ ---     │\n",
       "│ i64         ┆ i64      ┆ i64    ┆ str                 ┆   ┆ f64     ┆ str   ┆ str      ┆ str     │\n",
       "╞═════════════╪══════════╪════════╪═════════════════════╪═══╪═════════╪═══════╪══════════╪═════════╡\n",
       "│ 1           ┆ 0        ┆ 3      ┆ Braund, Mr. Owen    ┆ … ┆ 7.25    ┆ null  ┆ S        ┆ Titanic │\n",
       "│             ┆          ┆        ┆ Harris              ┆   ┆         ┆       ┆          ┆         │\n",
       "│ 2           ┆ 1        ┆ 1      ┆ Cumings, Mrs. John  ┆ … ┆ 71.2833 ┆ C85   ┆ C        ┆ Titanic │\n",
       "│             ┆          ┆        ┆ Bradley (Flor…      ┆   ┆         ┆       ┆          ┆         │\n",
       "│ 3           ┆ 1        ┆ 3      ┆ Heikkinen, Miss.    ┆ … ┆ 7.925   ┆ null  ┆ S        ┆ Titanic │\n",
       "│             ┆          ┆        ┆ Laina               ┆   ┆         ┆       ┆          ┆         │\n",
       "│ 4           ┆ 1        ┆ 1      ┆ Futrelle, Mrs.      ┆ … ┆ 53.1    ┆ C123  ┆ S        ┆ Titanic │\n",
       "│             ┆          ┆        ┆ Jacques Heath (Li…  ┆   ┆         ┆       ┆          ┆         │\n",
       "│ 5           ┆ 0        ┆ 3      ┆ Allen, Mr. William  ┆ … ┆ 8.05    ┆ null  ┆ S        ┆ Titanic │\n",
       "│             ┆          ┆        ┆ Henry               ┆   ┆         ┆       ┆          ┆         │\n",
       "└─────────────┴──────────┴────────┴─────────────────────┴───┴─────────┴───────┴──────────┴─────────┘"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.with_columns(pl.lit('Titanic')).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 13)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>index</th><th>PassengerId</th><th>Survived</th><th>Pclass</th><th>Name</th><th>Sex</th><th>Age</th><th>SibSp</th><th>Parch</th><th>Ticket</th><th>Fare</th><th>Cabin</th><th>Embarked</th></tr><tr><td>u32</td><td>i64</td><td>i64</td><td>i64</td><td>str</td><td>str</td><td>f64</td><td>i64</td><td>i64</td><td>str</td><td>f64</td><td>str</td><td>str</td></tr></thead><tbody><tr><td>0</td><td>1</td><td>0</td><td>3</td><td>&quot;Braund, Mr. Owen Harris&quot;</td><td>&quot;male&quot;</td><td>22.0</td><td>1</td><td>0</td><td>&quot;A/5 21171&quot;</td><td>7.25</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>1</td><td>2</td><td>1</td><td>1</td><td>&quot;Cumings, Mrs. John Bradley (Fl…</td><td>&quot;female&quot;</td><td>38.0</td><td>1</td><td>0</td><td>&quot;PC 17599&quot;</td><td>71.2833</td><td>&quot;C85&quot;</td><td>&quot;C&quot;</td></tr><tr><td>2</td><td>3</td><td>1</td><td>3</td><td>&quot;Heikkinen, Miss. Laina&quot;</td><td>&quot;female&quot;</td><td>26.0</td><td>0</td><td>0</td><td>&quot;STON/O2. 3101282&quot;</td><td>7.925</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>3</td><td>4</td><td>1</td><td>1</td><td>&quot;Futrelle, Mrs. Jacques Heath (…</td><td>&quot;female&quot;</td><td>35.0</td><td>1</td><td>0</td><td>&quot;113803&quot;</td><td>53.1</td><td>&quot;C123&quot;</td><td>&quot;S&quot;</td></tr><tr><td>4</td><td>5</td><td>0</td><td>3</td><td>&quot;Allen, Mr. William Henry&quot;</td><td>&quot;male&quot;</td><td>35.0</td><td>0</td><td>0</td><td>&quot;373450&quot;</td><td>8.05</td><td>null</td><td>&quot;S&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 13)\n",
       "┌───────┬─────────────┬──────────┬────────┬───┬──────────────────┬─────────┬───────┬──────────┐\n",
       "│ index ┆ PassengerId ┆ Survived ┆ Pclass ┆ … ┆ Ticket           ┆ Fare    ┆ Cabin ┆ Embarked │\n",
       "│ ---   ┆ ---         ┆ ---      ┆ ---    ┆   ┆ ---              ┆ ---     ┆ ---   ┆ ---      │\n",
       "│ u32   ┆ i64         ┆ i64      ┆ i64    ┆   ┆ str              ┆ f64     ┆ str   ┆ str      │\n",
       "╞═══════╪═════════════╪══════════╪════════╪═══╪══════════════════╪═════════╪═══════╪══════════╡\n",
       "│ 0     ┆ 1           ┆ 0        ┆ 3      ┆ … ┆ A/5 21171        ┆ 7.25    ┆ null  ┆ S        │\n",
       "│ 1     ┆ 2           ┆ 1        ┆ 1      ┆ … ┆ PC 17599         ┆ 71.2833 ┆ C85   ┆ C        │\n",
       "│ 2     ┆ 3           ┆ 1        ┆ 3      ┆ … ┆ STON/O2. 3101282 ┆ 7.925   ┆ null  ┆ S        │\n",
       "│ 3     ┆ 4           ┆ 1        ┆ 1      ┆ … ┆ 113803           ┆ 53.1    ┆ C123  ┆ S        │\n",
       "│ 4     ┆ 5           ┆ 0        ┆ 3      ┆ … ┆ 373450           ┆ 8.05    ┆ null  ┆ S        │\n",
       "└───────┴─────────────┴──────────┴────────┴───┴──────────────────┴─────────┴───────┴──────────┘"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.with_row_index().head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 12)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>PassengerId</th><th>Survived</th><th>Pclass</th><th>Name</th><th>Sex</th><th>Age</th><th>SibSp</th><th>Parch</th><th>Ticket</th><th>Fare</th><th>Cabin</th><th>Embarked</th></tr><tr><td>i64</td><td>i64</td><td>i64</td><td>str</td><td>str</td><td>f64</td><td>i64</td><td>i64</td><td>str</td><td>f64</td><td>str</td><td>str</td></tr></thead><tbody><tr><td>1</td><td>0</td><td>3</td><td>&quot;Braund, Mr. Ow…</td><td>&quot;Male&quot;</td><td>22.0</td><td>1</td><td>0</td><td>&quot;A/5 21171&quot;</td><td>7.25</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>2</td><td>1</td><td>1</td><td>&quot;Cumings, Mrs. …</td><td>&quot;Female&quot;</td><td>38.0</td><td>1</td><td>0</td><td>&quot;PC 17599&quot;</td><td>71.2833</td><td>&quot;C85&quot;</td><td>&quot;C&quot;</td></tr><tr><td>3</td><td>1</td><td>3</td><td>&quot;Heikkinen, Mis…</td><td>&quot;Female&quot;</td><td>26.0</td><td>0</td><td>0</td><td>&quot;STON/O2. 31012…</td><td>7.925</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>4</td><td>1</td><td>1</td><td>&quot;Futrelle, Mrs.…</td><td>&quot;Female&quot;</td><td>35.0</td><td>1</td><td>0</td><td>&quot;113803&quot;</td><td>53.1</td><td>&quot;C123&quot;</td><td>&quot;S&quot;</td></tr><tr><td>5</td><td>0</td><td>3</td><td>&quot;Allen, Mr. Wil…</td><td>&quot;Male&quot;</td><td>35.0</td><td>0</td><td>0</td><td>&quot;373450&quot;</td><td>8.05</td><td>null</td><td>&quot;S&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 12)\n",
       "┌─────────────┬──────────┬────────┬───────────────────┬───┬───────────┬─────────┬───────┬──────────┐\n",
       "│ PassengerId ┆ Survived ┆ Pclass ┆ Name              ┆ … ┆ Ticket    ┆ Fare    ┆ Cabin ┆ Embarked │\n",
       "│ ---         ┆ ---      ┆ ---    ┆ ---               ┆   ┆ ---       ┆ ---     ┆ ---   ┆ ---      │\n",
       "│ i64         ┆ i64      ┆ i64    ┆ str               ┆   ┆ str       ┆ f64     ┆ str   ┆ str      │\n",
       "╞═════════════╪══════════╪════════╪═══════════════════╪═══╪═══════════╪═════════╪═══════╪══════════╡\n",
       "│ 1           ┆ 0        ┆ 3      ┆ Braund, Mr. Owen  ┆ … ┆ A/5 21171 ┆ 7.25    ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Harris            ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 2           ┆ 1        ┆ 1      ┆ Cumings, Mrs.     ┆ … ┆ PC 17599  ┆ 71.2833 ┆ C85   ┆ C        │\n",
       "│             ┆          ┆        ┆ John Bradley      ┆   ┆           ┆         ┆       ┆          │\n",
       "│             ┆          ┆        ┆ (Flor…            ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 3           ┆ 1        ┆ 3      ┆ Heikkinen, Miss.  ┆ … ┆ STON/O2.  ┆ 7.925   ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Laina             ┆   ┆ 3101282   ┆         ┆       ┆          │\n",
       "│ 4           ┆ 1        ┆ 1      ┆ Futrelle, Mrs.    ┆ … ┆ 113803    ┆ 53.1    ┆ C123  ┆ S        │\n",
       "│             ┆          ┆        ┆ Jacques Heath     ┆   ┆           ┆         ┆       ┆          │\n",
       "│             ┆          ┆        ┆ (Li…              ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 5           ┆ 0        ┆ 3      ┆ Allen, Mr.        ┆ … ┆ 373450    ┆ 8.05    ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ William Henry     ┆   ┆           ┆         ┆       ┆          │\n",
       "└─────────────┴──────────┴────────┴───────────────────┴───┴───────────┴─────────┴───────┴──────────┘"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.with_columns(pl.col('Sex').str.to_titlecase()).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 12)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>PassengerId</th><th>Survived</th><th>Pclass</th><th>Name</th><th>Sex</th><th>Age</th><th>SibSp</th><th>Parch</th><th>Ticket</th><th>Fare</th><th>Cabin</th><th>Embarked</th></tr><tr><td>i64</td><td>i64</td><td>i64</td><td>str</td><td>str</td><td>f64</td><td>i64</td><td>i64</td><td>str</td><td>f64</td><td>str</td><td>str</td></tr></thead><tbody><tr><td>1</td><td>0</td><td>3</td><td>&quot;Braund, Mr. Ow…</td><td>&quot;male&quot;</td><td>22.0</td><td>1</td><td>0</td><td>&quot;A/5 21171&quot;</td><td>512.3292</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>2</td><td>1</td><td>1</td><td>&quot;Cumings, Mrs. …</td><td>&quot;female&quot;</td><td>38.0</td><td>1</td><td>0</td><td>&quot;PC 17599&quot;</td><td>512.3292</td><td>&quot;C85&quot;</td><td>&quot;C&quot;</td></tr><tr><td>3</td><td>1</td><td>3</td><td>&quot;Heikkinen, Mis…</td><td>&quot;female&quot;</td><td>26.0</td><td>0</td><td>0</td><td>&quot;STON/O2. 31012…</td><td>512.3292</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>4</td><td>1</td><td>1</td><td>&quot;Futrelle, Mrs.…</td><td>&quot;female&quot;</td><td>35.0</td><td>1</td><td>0</td><td>&quot;113803&quot;</td><td>512.3292</td><td>&quot;C123&quot;</td><td>&quot;S&quot;</td></tr><tr><td>5</td><td>0</td><td>3</td><td>&quot;Allen, Mr. Wil…</td><td>&quot;male&quot;</td><td>35.0</td><td>0</td><td>0</td><td>&quot;373450&quot;</td><td>512.3292</td><td>null</td><td>&quot;S&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 12)\n",
       "┌─────────────┬──────────┬────────┬──────────────────┬───┬───────────┬──────────┬───────┬──────────┐\n",
       "│ PassengerId ┆ Survived ┆ Pclass ┆ Name             ┆ … ┆ Ticket    ┆ Fare     ┆ Cabin ┆ Embarked │\n",
       "│ ---         ┆ ---      ┆ ---    ┆ ---              ┆   ┆ ---       ┆ ---      ┆ ---   ┆ ---      │\n",
       "│ i64         ┆ i64      ┆ i64    ┆ str              ┆   ┆ str       ┆ f64      ┆ str   ┆ str      │\n",
       "╞═════════════╪══════════╪════════╪══════════════════╪═══╪═══════════╪══════════╪═══════╪══════════╡\n",
       "│ 1           ┆ 0        ┆ 3      ┆ Braund, Mr. Owen ┆ … ┆ A/5 21171 ┆ 512.3292 ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Harris           ┆   ┆           ┆          ┆       ┆          │\n",
       "│ 2           ┆ 1        ┆ 1      ┆ Cumings, Mrs.    ┆ … ┆ PC 17599  ┆ 512.3292 ┆ C85   ┆ C        │\n",
       "│             ┆          ┆        ┆ John Bradley     ┆   ┆           ┆          ┆       ┆          │\n",
       "│             ┆          ┆        ┆ (Flor…           ┆   ┆           ┆          ┆       ┆          │\n",
       "│ 3           ┆ 1        ┆ 3      ┆ Heikkinen, Miss. ┆ … ┆ STON/O2.  ┆ 512.3292 ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Laina            ┆   ┆ 3101282   ┆          ┆       ┆          │\n",
       "│ 4           ┆ 1        ┆ 1      ┆ Futrelle, Mrs.   ┆ … ┆ 113803    ┆ 512.3292 ┆ C123  ┆ S        │\n",
       "│             ┆          ┆        ┆ Jacques Heath    ┆   ┆           ┆          ┆       ┆          │\n",
       "│             ┆          ┆        ┆ (Li…             ┆   ┆           ┆          ┆       ┆          │\n",
       "│ 5           ┆ 0        ┆ 3      ┆ Allen, Mr.       ┆ … ┆ 373450    ┆ 512.3292 ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ William Henry    ┆   ┆           ┆          ┆       ┆          │\n",
       "└─────────────┴──────────┴────────┴──────────────────┴───┴───────────┴──────────┴───────┴──────────┘"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.with_columns( \n",
    "    pl.col('Fare').max() \n",
    ").head() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 5)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>PassengerId</th><th>Survived</th><th>Sex</th><th>Age</th><th>Fare</th></tr><tr><td>i64</td><td>i64</td><td>str</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>1</td><td>0</td><td>&quot;male&quot;</td><td>22.0</td><td>7.25</td></tr><tr><td>2</td><td>1</td><td>&quot;female&quot;</td><td>38.0</td><td>71.2833</td></tr><tr><td>3</td><td>1</td><td>&quot;female&quot;</td><td>26.0</td><td>7.925</td></tr><tr><td>4</td><td>1</td><td>&quot;female&quot;</td><td>35.0</td><td>53.1</td></tr><tr><td>5</td><td>0</td><td>&quot;male&quot;</td><td>35.0</td><td>8.05</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 5)\n",
       "┌─────────────┬──────────┬────────┬──────┬─────────┐\n",
       "│ PassengerId ┆ Survived ┆ Sex    ┆ Age  ┆ Fare    │\n",
       "│ ---         ┆ ---      ┆ ---    ┆ ---  ┆ ---     │\n",
       "│ i64         ┆ i64      ┆ str    ┆ f64  ┆ f64     │\n",
       "╞═════════════╪══════════╪════════╪══════╪═════════╡\n",
       "│ 1           ┆ 0        ┆ male   ┆ 22.0 ┆ 7.25    │\n",
       "│ 2           ┆ 1        ┆ female ┆ 38.0 ┆ 71.2833 │\n",
       "│ 3           ┆ 1        ┆ female ┆ 26.0 ┆ 7.925   │\n",
       "│ 4           ┆ 1        ┆ female ┆ 35.0 ┆ 53.1    │\n",
       "│ 5           ┆ 0        ┆ male   ┆ 35.0 ┆ 8.05    │\n",
       "└─────────────┴──────────┴────────┴──────┴─────────┘"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.drop(['Pclass', 'Name', 'SibSp', 'Parch', 'Ticket', 'Cabin', 'Embarked']).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 5)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>PassengerId</th><th>Survived</th><th>Sex</th><th>Age</th><th>Fare</th></tr><tr><td>i64</td><td>i64</td><td>str</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>1</td><td>0</td><td>&quot;male&quot;</td><td>22.0</td><td>7.25</td></tr><tr><td>2</td><td>1</td><td>&quot;female&quot;</td><td>38.0</td><td>71.2833</td></tr><tr><td>3</td><td>1</td><td>&quot;female&quot;</td><td>26.0</td><td>7.925</td></tr><tr><td>4</td><td>1</td><td>&quot;female&quot;</td><td>35.0</td><td>53.1</td></tr><tr><td>5</td><td>0</td><td>&quot;male&quot;</td><td>35.0</td><td>8.05</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 5)\n",
       "┌─────────────┬──────────┬────────┬──────┬─────────┐\n",
       "│ PassengerId ┆ Survived ┆ Sex    ┆ Age  ┆ Fare    │\n",
       "│ ---         ┆ ---      ┆ ---    ┆ ---  ┆ ---     │\n",
       "│ i64         ┆ i64      ┆ str    ┆ f64  ┆ f64     │\n",
       "╞═════════════╪══════════╪════════╪══════╪═════════╡\n",
       "│ 1           ┆ 0        ┆ male   ┆ 22.0 ┆ 7.25    │\n",
       "│ 2           ┆ 1        ┆ female ┆ 38.0 ┆ 71.2833 │\n",
       "│ 3           ┆ 1        ┆ female ┆ 26.0 ┆ 7.925   │\n",
       "│ 4           ┆ 1        ┆ female ┆ 35.0 ┆ 53.1    │\n",
       "│ 5           ┆ 0        ┆ male   ┆ 35.0 ┆ 8.05    │\n",
       "└─────────────┴──────────┴────────┴──────┴─────────┘"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.select(['PassengerId', 'Survived', 'Sex', 'Age', 'Fare']).head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There is more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 14)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>PassengerId</th><th>Survived</th><th>Pclass</th><th>Name</th><th>Sex</th><th>Age</th><th>SibSp</th><th>Parch</th><th>Ticket</th><th>Fare</th><th>Cabin</th><th>Embarked</th><th>Max Fare</th><th>literal</th></tr><tr><td>i64</td><td>i64</td><td>i64</td><td>str</td><td>str</td><td>f64</td><td>i64</td><td>i64</td><td>str</td><td>f64</td><td>str</td><td>str</td><td>f64</td><td>str</td></tr></thead><tbody><tr><td>1</td><td>0</td><td>3</td><td>&quot;Braund, Mr. Ow…</td><td>&quot;Male&quot;</td><td>22.0</td><td>1</td><td>0</td><td>&quot;A/5 21171&quot;</td><td>7.25</td><td>null</td><td>&quot;S&quot;</td><td>512.3292</td><td>&quot;Titanic&quot;</td></tr><tr><td>2</td><td>1</td><td>1</td><td>&quot;Cumings, Mrs. …</td><td>&quot;Female&quot;</td><td>38.0</td><td>1</td><td>0</td><td>&quot;PC 17599&quot;</td><td>71.2833</td><td>&quot;C85&quot;</td><td>&quot;C&quot;</td><td>512.3292</td><td>&quot;Titanic&quot;</td></tr><tr><td>3</td><td>1</td><td>3</td><td>&quot;Heikkinen, Mis…</td><td>&quot;Female&quot;</td><td>26.0</td><td>0</td><td>0</td><td>&quot;STON/O2. 31012…</td><td>7.925</td><td>null</td><td>&quot;S&quot;</td><td>512.3292</td><td>&quot;Titanic&quot;</td></tr><tr><td>4</td><td>1</td><td>1</td><td>&quot;Futrelle, Mrs.…</td><td>&quot;Female&quot;</td><td>35.0</td><td>1</td><td>0</td><td>&quot;113803&quot;</td><td>53.1</td><td>&quot;C123&quot;</td><td>&quot;S&quot;</td><td>512.3292</td><td>&quot;Titanic&quot;</td></tr><tr><td>5</td><td>0</td><td>3</td><td>&quot;Allen, Mr. Wil…</td><td>&quot;Male&quot;</td><td>35.0</td><td>0</td><td>0</td><td>&quot;373450&quot;</td><td>8.05</td><td>null</td><td>&quot;S&quot;</td><td>512.3292</td><td>&quot;Titanic&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 14)\n",
       "┌─────────────┬──────────┬────────┬────────────────────┬───┬───────┬──────────┬──────────┬─────────┐\n",
       "│ PassengerId ┆ Survived ┆ Pclass ┆ Name               ┆ … ┆ Cabin ┆ Embarked ┆ Max Fare ┆ literal │\n",
       "│ ---         ┆ ---      ┆ ---    ┆ ---                ┆   ┆ ---   ┆ ---      ┆ ---      ┆ ---     │\n",
       "│ i64         ┆ i64      ┆ i64    ┆ str                ┆   ┆ str   ┆ str      ┆ f64      ┆ str     │\n",
       "╞═════════════╪══════════╪════════╪════════════════════╪═══╪═══════╪══════════╪══════════╪═════════╡\n",
       "│ 1           ┆ 0        ┆ 3      ┆ Braund, Mr. Owen   ┆ … ┆ null  ┆ S        ┆ 512.3292 ┆ Titanic │\n",
       "│             ┆          ┆        ┆ Harris             ┆   ┆       ┆          ┆          ┆         │\n",
       "│ 2           ┆ 1        ┆ 1      ┆ Cumings, Mrs. John ┆ … ┆ C85   ┆ C        ┆ 512.3292 ┆ Titanic │\n",
       "│             ┆          ┆        ┆ Bradley (Flor…     ┆   ┆       ┆          ┆          ┆         │\n",
       "│ 3           ┆ 1        ┆ 3      ┆ Heikkinen, Miss.   ┆ … ┆ null  ┆ S        ┆ 512.3292 ┆ Titanic │\n",
       "│             ┆          ┆        ┆ Laina              ┆   ┆       ┆          ┆          ┆         │\n",
       "│ 4           ┆ 1        ┆ 1      ┆ Futrelle, Mrs.     ┆ … ┆ C123  ┆ S        ┆ 512.3292 ┆ Titanic │\n",
       "│             ┆          ┆        ┆ Jacques Heath (Li… ┆   ┆       ┆          ┆          ┆         │\n",
       "│ 5           ┆ 0        ┆ 3      ┆ Allen, Mr. William ┆ … ┆ null  ┆ S        ┆ 512.3292 ┆ Titanic │\n",
       "│             ┆          ┆        ┆ Henry              ┆   ┆       ┆          ┆          ┆         │\n",
       "└─────────────┴──────────┴────────┴────────────────────┴───┴───────┴──────────┴──────────┴─────────┘"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "best_practice = (\n",
    "    df.with_columns(\n",
    "        pl.col('Fare').max().alias('Max Fare'),\n",
    "        pl.lit('Titanic'),\n",
    "        pl.col('Sex').str.to_titlecase()\n",
    "    )\n",
    ")\n",
    "best_practice.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 14)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>PassengerId</th><th>Survived</th><th>Pclass</th><th>Name</th><th>Sex</th><th>Age</th><th>SibSp</th><th>Parch</th><th>Ticket</th><th>Fare</th><th>Cabin</th><th>Embarked</th><th>Max Fare</th><th>literal</th></tr><tr><td>i64</td><td>i64</td><td>i64</td><td>str</td><td>str</td><td>f64</td><td>i64</td><td>i64</td><td>str</td><td>f64</td><td>str</td><td>str</td><td>f64</td><td>str</td></tr></thead><tbody><tr><td>1</td><td>0</td><td>3</td><td>&quot;Braund, Mr. Ow…</td><td>&quot;Male&quot;</td><td>22.0</td><td>1</td><td>0</td><td>&quot;A/5 21171&quot;</td><td>7.25</td><td>null</td><td>&quot;S&quot;</td><td>512.3292</td><td>&quot;Titanic&quot;</td></tr><tr><td>2</td><td>1</td><td>1</td><td>&quot;Cumings, Mrs. …</td><td>&quot;Female&quot;</td><td>38.0</td><td>1</td><td>0</td><td>&quot;PC 17599&quot;</td><td>71.2833</td><td>&quot;C85&quot;</td><td>&quot;C&quot;</td><td>512.3292</td><td>&quot;Titanic&quot;</td></tr><tr><td>3</td><td>1</td><td>3</td><td>&quot;Heikkinen, Mis…</td><td>&quot;Female&quot;</td><td>26.0</td><td>0</td><td>0</td><td>&quot;STON/O2. 31012…</td><td>7.925</td><td>null</td><td>&quot;S&quot;</td><td>512.3292</td><td>&quot;Titanic&quot;</td></tr><tr><td>4</td><td>1</td><td>1</td><td>&quot;Futrelle, Mrs.…</td><td>&quot;Female&quot;</td><td>35.0</td><td>1</td><td>0</td><td>&quot;113803&quot;</td><td>53.1</td><td>&quot;C123&quot;</td><td>&quot;S&quot;</td><td>512.3292</td><td>&quot;Titanic&quot;</td></tr><tr><td>5</td><td>0</td><td>3</td><td>&quot;Allen, Mr. Wil…</td><td>&quot;Male&quot;</td><td>35.0</td><td>0</td><td>0</td><td>&quot;373450&quot;</td><td>8.05</td><td>null</td><td>&quot;S&quot;</td><td>512.3292</td><td>&quot;Titanic&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 14)\n",
       "┌─────────────┬──────────┬────────┬────────────────────┬───┬───────┬──────────┬──────────┬─────────┐\n",
       "│ PassengerId ┆ Survived ┆ Pclass ┆ Name               ┆ … ┆ Cabin ┆ Embarked ┆ Max Fare ┆ literal │\n",
       "│ ---         ┆ ---      ┆ ---    ┆ ---                ┆   ┆ ---   ┆ ---      ┆ ---      ┆ ---     │\n",
       "│ i64         ┆ i64      ┆ i64    ┆ str                ┆   ┆ str   ┆ str      ┆ f64      ┆ str     │\n",
       "╞═════════════╪══════════╪════════╪════════════════════╪═══╪═══════╪══════════╪══════════╪═════════╡\n",
       "│ 1           ┆ 0        ┆ 3      ┆ Braund, Mr. Owen   ┆ … ┆ null  ┆ S        ┆ 512.3292 ┆ Titanic │\n",
       "│             ┆          ┆        ┆ Harris             ┆   ┆       ┆          ┆          ┆         │\n",
       "│ 2           ┆ 1        ┆ 1      ┆ Cumings, Mrs. John ┆ … ┆ C85   ┆ C        ┆ 512.3292 ┆ Titanic │\n",
       "│             ┆          ┆        ┆ Bradley (Flor…     ┆   ┆       ┆          ┆          ┆         │\n",
       "│ 3           ┆ 1        ┆ 3      ┆ Heikkinen, Miss.   ┆ … ┆ null  ┆ S        ┆ 512.3292 ┆ Titanic │\n",
       "│             ┆          ┆        ┆ Laina              ┆   ┆       ┆          ┆          ┆         │\n",
       "│ 4           ┆ 1        ┆ 1      ┆ Futrelle, Mrs.     ┆ … ┆ C123  ┆ S        ┆ 512.3292 ┆ Titanic │\n",
       "│             ┆          ┆        ┆ Jacques Heath (Li… ┆   ┆       ┆          ┆          ┆         │\n",
       "│ 5           ┆ 0        ┆ 3      ┆ Allen, Mr. William ┆ … ┆ null  ┆ S        ┆ 512.3292 ┆ Titanic │\n",
       "│             ┆          ┆        ┆ Henry              ┆   ┆       ┆          ┆          ┆         │\n",
       "└─────────────┴──────────┴────────┴────────────────────┴───┴───────┴──────────┴──────────┴─────────┘"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "not_so_good_practice = (\n",
    "    df\n",
    "    .with_columns(pl.col('Fare').max().alias('Max Fare'))\n",
    "    .with_columns(pl.lit('Titanic'))\n",
    "    .with_columns(pl.col('Sex').str.to_titlecase())\n",
    ")\n",
    "not_so_good_practice.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Method chaining"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 12)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>PassengerId</th><th>Survived</th><th>Pclass</th><th>Name</th><th>Sex</th><th>Age</th><th>SibSp</th><th>Parch</th><th>Ticket</th><th>Fare</th><th>Cabin</th><th>Embarked</th></tr><tr><td>i64</td><td>i64</td><td>i64</td><td>str</td><td>str</td><td>f64</td><td>i64</td><td>i64</td><td>str</td><td>f64</td><td>str</td><td>str</td></tr></thead><tbody><tr><td>1</td><td>0</td><td>3</td><td>&quot;Braund, Mr. Ow…</td><td>&quot;male&quot;</td><td>22.0</td><td>1</td><td>0</td><td>&quot;A/5 21171&quot;</td><td>7.25</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>2</td><td>1</td><td>1</td><td>&quot;Cumings, Mrs. …</td><td>&quot;female&quot;</td><td>38.0</td><td>1</td><td>0</td><td>&quot;PC 17599&quot;</td><td>71.2833</td><td>&quot;C85&quot;</td><td>&quot;C&quot;</td></tr><tr><td>3</td><td>1</td><td>3</td><td>&quot;Heikkinen, Mis…</td><td>&quot;female&quot;</td><td>26.0</td><td>0</td><td>0</td><td>&quot;STON/O2. 31012…</td><td>7.925</td><td>null</td><td>&quot;S&quot;</td></tr><tr><td>4</td><td>1</td><td>1</td><td>&quot;Futrelle, Mrs.…</td><td>&quot;female&quot;</td><td>35.0</td><td>1</td><td>0</td><td>&quot;113803&quot;</td><td>53.1</td><td>&quot;C123&quot;</td><td>&quot;S&quot;</td></tr><tr><td>5</td><td>0</td><td>3</td><td>&quot;Allen, Mr. Wil…</td><td>&quot;male&quot;</td><td>35.0</td><td>0</td><td>0</td><td>&quot;373450&quot;</td><td>8.05</td><td>null</td><td>&quot;S&quot;</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 12)\n",
       "┌─────────────┬──────────┬────────┬───────────────────┬───┬───────────┬─────────┬───────┬──────────┐\n",
       "│ PassengerId ┆ Survived ┆ Pclass ┆ Name              ┆ … ┆ Ticket    ┆ Fare    ┆ Cabin ┆ Embarked │\n",
       "│ ---         ┆ ---      ┆ ---    ┆ ---               ┆   ┆ ---       ┆ ---     ┆ ---   ┆ ---      │\n",
       "│ i64         ┆ i64      ┆ i64    ┆ str               ┆   ┆ str       ┆ f64     ┆ str   ┆ str      │\n",
       "╞═════════════╪══════════╪════════╪═══════════════════╪═══╪═══════════╪═════════╪═══════╪══════════╡\n",
       "│ 1           ┆ 0        ┆ 3      ┆ Braund, Mr. Owen  ┆ … ┆ A/5 21171 ┆ 7.25    ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Harris            ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 2           ┆ 1        ┆ 1      ┆ Cumings, Mrs.     ┆ … ┆ PC 17599  ┆ 71.2833 ┆ C85   ┆ C        │\n",
       "│             ┆          ┆        ┆ John Bradley      ┆   ┆           ┆         ┆       ┆          │\n",
       "│             ┆          ┆        ┆ (Flor…            ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 3           ┆ 1        ┆ 3      ┆ Heikkinen, Miss.  ┆ … ┆ STON/O2.  ┆ 7.925   ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ Laina             ┆   ┆ 3101282   ┆         ┆       ┆          │\n",
       "│ 4           ┆ 1        ┆ 1      ┆ Futrelle, Mrs.    ┆ … ┆ 113803    ┆ 53.1    ┆ C123  ┆ S        │\n",
       "│             ┆          ┆        ┆ Jacques Heath     ┆   ┆           ┆         ┆       ┆          │\n",
       "│             ┆          ┆        ┆ (Li…              ┆   ┆           ┆         ┆       ┆          │\n",
       "│ 5           ┆ 0        ┆ 3      ┆ Allen, Mr.        ┆ … ┆ 373450    ┆ 8.05    ┆ null  ┆ S        │\n",
       "│             ┆          ┆        ┆ William Henry     ┆   ┆           ┆         ┆       ┆          │\n",
       "└─────────────┴──────────┴────────┴───────────────────┴───┴───────────┴─────────┴───────┴──────────┘"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pl.read_csv('../data/titanic_dataset.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "cols = ['Name', 'Sex', 'Age', 'Fare', 'Cabin', 'Pclass', 'Survived']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 7)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Name</th><th>Sex</th><th>Age</th><th>Fare</th><th>Cabin</th><th>Pclass</th><th>Survived</th></tr><tr><td>str</td><td>str</td><td>f64</td><td>f64</td><td>str</td><td>i64</td><td>i64</td></tr></thead><tbody><tr><td>&quot;Abbott, Mrs. S…</td><td>&quot;female&quot;</td><td>35.0</td><td>20.25</td><td>null</td><td>3</td><td>1</td></tr><tr><td>&quot;Allen, Mr. Wil…</td><td>&quot;male&quot;</td><td>35.0</td><td>8.05</td><td>null</td><td>3</td><td>0</td></tr><tr><td>&quot;Asim, Mr. Adol…</td><td>&quot;male&quot;</td><td>35.0</td><td>7.05</td><td>null</td><td>3</td><td>0</td></tr><tr><td>&quot;Bissette, Miss…</td><td>&quot;female&quot;</td><td>35.0</td><td>135.6333</td><td>&quot;C99&quot;</td><td>1</td><td>1</td></tr><tr><td>&quot;Brocklebank, M…</td><td>&quot;male&quot;</td><td>35.0</td><td>8.05</td><td>null</td><td>3</td><td>0</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 7)\n",
       "┌──────────────────────────────────┬────────┬──────┬──────────┬───────┬────────┬──────────┐\n",
       "│ Name                             ┆ Sex    ┆ Age  ┆ Fare     ┆ Cabin ┆ Pclass ┆ Survived │\n",
       "│ ---                              ┆ ---    ┆ ---  ┆ ---      ┆ ---   ┆ ---    ┆ ---      │\n",
       "│ str                              ┆ str    ┆ f64  ┆ f64      ┆ str   ┆ i64    ┆ i64      │\n",
       "╞══════════════════════════════════╪════════╪══════╪══════════╪═══════╪════════╪══════════╡\n",
       "│ Abbott, Mrs. Stanton (Rosa Hunt) ┆ female ┆ 35.0 ┆ 20.25    ┆ null  ┆ 3      ┆ 1        │\n",
       "│ Allen, Mr. William Henry         ┆ male   ┆ 35.0 ┆ 8.05     ┆ null  ┆ 3      ┆ 0        │\n",
       "│ Asim, Mr. Adola                  ┆ male   ┆ 35.0 ┆ 7.05     ┆ null  ┆ 3      ┆ 0        │\n",
       "│ Bissette, Miss. Amelia           ┆ female ┆ 35.0 ┆ 135.6333 ┆ C99   ┆ 1      ┆ 1        │\n",
       "│ Brocklebank, Mr. William Alfred  ┆ male   ┆ 35.0 ┆ 8.05     ┆ null  ┆ 3      ┆ 0        │\n",
       "└──────────────────────────────────┴────────┴──────┴──────────┴───────┴────────┴──────────┘"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = df.select(cols)\n",
    "df = df.filter(pl.col('Age')>=35)\n",
    "df = df.sort(by=['Age', 'Name'])\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 7)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Name</th><th>Sex</th><th>Age</th><th>Fare</th><th>Cabin</th><th>Pclass</th><th>Survived</th></tr><tr><td>str</td><td>str</td><td>f64</td><td>f64</td><td>str</td><td>i64</td><td>i64</td></tr></thead><tbody><tr><td>&quot;Abbott, Mrs. S…</td><td>&quot;female&quot;</td><td>35.0</td><td>20.25</td><td>null</td><td>3</td><td>1</td></tr><tr><td>&quot;Allen, Mr. Wil…</td><td>&quot;male&quot;</td><td>35.0</td><td>8.05</td><td>null</td><td>3</td><td>0</td></tr><tr><td>&quot;Asim, Mr. Adol…</td><td>&quot;male&quot;</td><td>35.0</td><td>7.05</td><td>null</td><td>3</td><td>0</td></tr><tr><td>&quot;Bissette, Miss…</td><td>&quot;female&quot;</td><td>35.0</td><td>135.6333</td><td>&quot;C99&quot;</td><td>1</td><td>1</td></tr><tr><td>&quot;Brocklebank, M…</td><td>&quot;male&quot;</td><td>35.0</td><td>8.05</td><td>null</td><td>3</td><td>0</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 7)\n",
       "┌──────────────────────────────────┬────────┬──────┬──────────┬───────┬────────┬──────────┐\n",
       "│ Name                             ┆ Sex    ┆ Age  ┆ Fare     ┆ Cabin ┆ Pclass ┆ Survived │\n",
       "│ ---                              ┆ ---    ┆ ---  ┆ ---      ┆ ---   ┆ ---    ┆ ---      │\n",
       "│ str                              ┆ str    ┆ f64  ┆ f64      ┆ str   ┆ i64    ┆ i64      │\n",
       "╞══════════════════════════════════╪════════╪══════╪══════════╪═══════╪════════╪══════════╡\n",
       "│ Abbott, Mrs. Stanton (Rosa Hunt) ┆ female ┆ 35.0 ┆ 20.25    ┆ null  ┆ 3      ┆ 1        │\n",
       "│ Allen, Mr. William Henry         ┆ male   ┆ 35.0 ┆ 8.05     ┆ null  ┆ 3      ┆ 0        │\n",
       "│ Asim, Mr. Adola                  ┆ male   ┆ 35.0 ┆ 7.05     ┆ null  ┆ 3      ┆ 0        │\n",
       "│ Bissette, Miss. Amelia           ┆ female ┆ 35.0 ┆ 135.6333 ┆ C99   ┆ 1      ┆ 1        │\n",
       "│ Brocklebank, Mr. William Alfred  ┆ male   ┆ 35.0 ┆ 8.05     ┆ null  ┆ 3      ┆ 0        │\n",
       "└──────────────────────────────────┴────────┴──────┴──────────┴───────┴────────┴──────────┘"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = df.select(cols).filter(pl.col('Age')>=35).sort(by=['Age', 'Name'])\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 7)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Name</th><th>Sex</th><th>Age</th><th>Fare</th><th>Cabin</th><th>Pclass</th><th>Survived</th></tr><tr><td>str</td><td>str</td><td>f64</td><td>f64</td><td>str</td><td>i64</td><td>i64</td></tr></thead><tbody><tr><td>&quot;Abbott, Mrs. S…</td><td>&quot;female&quot;</td><td>35.0</td><td>20.25</td><td>null</td><td>3</td><td>1</td></tr><tr><td>&quot;Allen, Mr. Wil…</td><td>&quot;male&quot;</td><td>35.0</td><td>8.05</td><td>null</td><td>3</td><td>0</td></tr><tr><td>&quot;Asim, Mr. Adol…</td><td>&quot;male&quot;</td><td>35.0</td><td>7.05</td><td>null</td><td>3</td><td>0</td></tr><tr><td>&quot;Bissette, Miss…</td><td>&quot;female&quot;</td><td>35.0</td><td>135.6333</td><td>&quot;C99&quot;</td><td>1</td><td>1</td></tr><tr><td>&quot;Brocklebank, M…</td><td>&quot;male&quot;</td><td>35.0</td><td>8.05</td><td>null</td><td>3</td><td>0</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 7)\n",
       "┌──────────────────────────────────┬────────┬──────┬──────────┬───────┬────────┬──────────┐\n",
       "│ Name                             ┆ Sex    ┆ Age  ┆ Fare     ┆ Cabin ┆ Pclass ┆ Survived │\n",
       "│ ---                              ┆ ---    ┆ ---  ┆ ---      ┆ ---   ┆ ---    ┆ ---      │\n",
       "│ str                              ┆ str    ┆ f64  ┆ f64      ┆ str   ┆ i64    ┆ i64      │\n",
       "╞══════════════════════════════════╪════════╪══════╪══════════╪═══════╪════════╪══════════╡\n",
       "│ Abbott, Mrs. Stanton (Rosa Hunt) ┆ female ┆ 35.0 ┆ 20.25    ┆ null  ┆ 3      ┆ 1        │\n",
       "│ Allen, Mr. William Henry         ┆ male   ┆ 35.0 ┆ 8.05     ┆ null  ┆ 3      ┆ 0        │\n",
       "│ Asim, Mr. Adola                  ┆ male   ┆ 35.0 ┆ 7.05     ┆ null  ┆ 3      ┆ 0        │\n",
       "│ Bissette, Miss. Amelia           ┆ female ┆ 35.0 ┆ 135.6333 ┆ C99   ┆ 1      ┆ 1        │\n",
       "│ Brocklebank, Mr. William Alfred  ┆ male   ┆ 35.0 ┆ 8.05     ┆ null  ┆ 3      ┆ 0        │\n",
       "└──────────────────────────────────┴────────┴──────┴──────────┴───────┴────────┴──────────┘"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = (\n",
    "    df\n",
    "    .select(cols)\n",
    "    .filter(pl.col('Age')>=35)\n",
    "    .sort(by=['Age', 'Name'])\n",
    ")\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 7)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Name</th><th>Sex</th><th>Age</th><th>Fare</th><th>Cabin</th><th>Pclass</th><th>Survived</th></tr><tr><td>str</td><td>str</td><td>f64</td><td>f64</td><td>str</td><td>i64</td><td>i64</td></tr></thead><tbody><tr><td>&quot;Abbott, Mrs. S…</td><td>&quot;female&quot;</td><td>35.0</td><td>20.25</td><td>null</td><td>3</td><td>1</td></tr><tr><td>&quot;Allen, Mr. Wil…</td><td>&quot;male&quot;</td><td>35.0</td><td>8.05</td><td>null</td><td>3</td><td>0</td></tr><tr><td>&quot;Asim, Mr. Adol…</td><td>&quot;male&quot;</td><td>35.0</td><td>7.05</td><td>null</td><td>3</td><td>0</td></tr><tr><td>&quot;Bissette, Miss…</td><td>&quot;female&quot;</td><td>35.0</td><td>135.6333</td><td>&quot;C99&quot;</td><td>1</td><td>1</td></tr><tr><td>&quot;Brocklebank, M…</td><td>&quot;male&quot;</td><td>35.0</td><td>8.05</td><td>null</td><td>3</td><td>0</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 7)\n",
       "┌──────────────────────────────────┬────────┬──────┬──────────┬───────┬────────┬──────────┐\n",
       "│ Name                             ┆ Sex    ┆ Age  ┆ Fare     ┆ Cabin ┆ Pclass ┆ Survived │\n",
       "│ ---                              ┆ ---    ┆ ---  ┆ ---      ┆ ---   ┆ ---    ┆ ---      │\n",
       "│ str                              ┆ str    ┆ f64  ┆ f64      ┆ str   ┆ i64    ┆ i64      │\n",
       "╞══════════════════════════════════╪════════╪══════╪══════════╪═══════╪════════╪══════════╡\n",
       "│ Abbott, Mrs. Stanton (Rosa Hunt) ┆ female ┆ 35.0 ┆ 20.25    ┆ null  ┆ 3      ┆ 1        │\n",
       "│ Allen, Mr. William Henry         ┆ male   ┆ 35.0 ┆ 8.05     ┆ null  ┆ 3      ┆ 0        │\n",
       "│ Asim, Mr. Adola                  ┆ male   ┆ 35.0 ┆ 7.05     ┆ null  ┆ 3      ┆ 0        │\n",
       "│ Bissette, Miss. Amelia           ┆ female ┆ 35.0 ┆ 135.6333 ┆ C99   ┆ 1      ┆ 1        │\n",
       "│ Brocklebank, Mr. William Alfred  ┆ male   ┆ 35.0 ┆ 8.05     ┆ null  ┆ 3      ┆ 0        │\n",
       "└──────────────────────────────────┴────────┴──────┴──────────┴───────┴────────┴──────────┘"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = (\n",
    "    df\n",
    "    .select(cols)\n",
    "    # .filter(pl.col('Age')>=35)\n",
    "    .sort(by=['Age', 'Name'])\n",
    ")\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There is more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df \\\n",
    "    .select(cols) \\\n",
    "    .filter(pl.col('Age')>=35) \\\n",
    "    .sort(by=['Age', 'Name']) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Processing datasets larger than RAM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import polars as pl "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 23)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Trip ID</th><th>Taxi ID</th><th>Trip Start Timestamp</th><th>Trip End Timestamp</th><th>Trip Seconds</th><th>Trip Miles</th><th>Pickup Census Tract</th><th>Dropoff Census Tract</th><th>Pickup Community Area</th><th>Dropoff Community Area</th><th>Fare</th><th>Tips</th><th>Tolls</th><th>Extras</th><th>Trip Total</th><th>Payment Type</th><th>Company</th><th>Pickup Centroid Latitude</th><th>Pickup Centroid Longitude</th><th>Pickup Centroid Location</th><th>Dropoff Centroid Latitude</th><th>Dropoff Centroid Longitude</th><th>Dropoff Centroid  Location</th></tr><tr><td>str</td><td>str</td><td>str</td><td>str</td><td>i64</td><td>f64</td><td>i64</td><td>i64</td><td>i64</td><td>i64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>f64</td><td>str</td><td>str</td><td>f64</td><td>f64</td><td>str</td><td>f64</td><td>f64</td><td>str</td></tr></thead><tbody><tr><td>&quot;2c4c7c96c03236…</td><td>&quot;f68fe1183e6d87…</td><td>&quot;10/23/2016 10:…</td><td>&quot;10/23/2016 10:…</td><td>360</td><td>1.1</td><td>17031320100</td><td>17031320100</td><td>32</td><td>32</td><td>6.75</td><td>0.0</td><td>0.0</td><td>1.5</td><td>8.25</td><td>&quot;Cash&quot;</td><td>&quot;Medallion Leas…</td><td>41.884987</td><td>-87.620993</td><td>&quot;POINT (-87.620…</td><td>41.884987</td><td>-87.620993</td><td>&quot;POINT (-87.620…</td></tr><tr><td>&quot;d3a30f8612e040…</td><td>&quot;97ca0053c70788…</td><td>&quot;10/23/2016 07:…</td><td>&quot;10/23/2016 07:…</td><td>300</td><td>1.0</td><td>17031320100</td><td>17031081700</td><td>32</td><td>8</td><td>6.25</td><td>0.0</td><td>0.0</td><td>0.0</td><td>6.25</td><td>&quot;Cash&quot;</td><td>&quot;Medallion Leas…</td><td>41.884987</td><td>-87.620993</td><td>&quot;POINT (-87.620…</td><td>41.892042</td><td>-87.631864</td><td>&quot;POINT (-87.631…</td></tr><tr><td>&quot;95178b9e52c980…</td><td>&quot;f0b96329a7e390…</td><td>&quot;10/27/2016 08:…</td><td>&quot;10/27/2016 09:…</td><td>636</td><td>2.23</td><td>17031833100</td><td>17031081403</td><td>28</td><td>8</td><td>9.75</td><td>0.0</td><td>0.0</td><td>0.0</td><td>9.75</td><td>&quot;Cash&quot;</td><td>&quot;City Service&quot;</td><td>41.879067</td><td>-87.657005</td><td>&quot;POINT (-87.657…</td><td>41.890922</td><td>-87.618868</td><td>&quot;POINT (-87.618…</td></tr><tr><td>&quot;b221f753f4fce4…</td><td>&quot;11b7c42b4edd22…</td><td>&quot;10/27/2016 08:…</td><td>&quot;10/27/2016 08:…</td><td>1000</td><td>2.13</td><td>17031320600</td><td>17031330100</td><td>32</td><td>33</td><td>10.75</td><td>0.0</td><td>0.0</td><td>1.5</td><td>12.25</td><td>&quot;Cash&quot;</td><td>&quot;Chicago Carria…</td><td>41.870607</td><td>-87.622173</td><td>&quot;POINT (-87.622…</td><td>41.85935</td><td>-87.617358</td><td>&quot;POINT (-87.617…</td></tr><tr><td>&quot;d6d73a306f0e46…</td><td>&quot;d49495206048d4…</td><td>&quot;10/27/2016 05:…</td><td>&quot;10/27/2016 05:…</td><td>540</td><td>1.5</td><td>17031081900</td><td>17031833000</td><td>8</td><td>28</td><td>7.5</td><td>0.0</td><td>0.0</td><td>0.0</td><td>7.5</td><td>&quot;Cash&quot;</td><td>&quot;Taxi Affiliati…</td><td>41.897984</td><td>-87.641492</td><td>&quot;POINT (-87.641…</td><td>41.885281</td><td>-87.657233</td><td>&quot;POINT (-87.657…</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 23)\n",
       "┌───────────┬───────────┬───────────┬───────────┬───┬───────────┬───────────┬───────────┬──────────┐\n",
       "│ Trip ID   ┆ Taxi ID   ┆ Trip      ┆ Trip End  ┆ … ┆ Pickup    ┆ Dropoff   ┆ Dropoff   ┆ Dropoff  │\n",
       "│ ---       ┆ ---       ┆ Start     ┆ Timestamp ┆   ┆ Centroid  ┆ Centroid  ┆ Centroid  ┆ Centroid │\n",
       "│ str       ┆ str       ┆ Timestamp ┆ ---       ┆   ┆ Location  ┆ Latitude  ┆ Longitude ┆ Location │\n",
       "│           ┆           ┆ ---       ┆ str       ┆   ┆ ---       ┆ ---       ┆ ---       ┆ ---      │\n",
       "│           ┆           ┆ str       ┆           ┆   ┆ str       ┆ f64       ┆ f64       ┆ str      │\n",
       "╞═══════════╪═══════════╪═══════════╪═══════════╪═══╪═══════════╪═══════════╪═══════════╪══════════╡\n",
       "│ 2c4c7c96c ┆ f68fe1183 ┆ 10/23/201 ┆ 10/23/201 ┆ … ┆ POINT (-8 ┆ 41.884987 ┆ -87.62099 ┆ POINT    │\n",
       "│ 03236d94e ┆ e6d87a12b ┆ 6         ┆ 6         ┆   ┆ 7.6209929 ┆           ┆ 3         ┆ (-87.620 │\n",
       "│ f20192a33 ┆ d7db1c367 ┆ 10:15:00  ┆ 10:15:00  ┆   ┆ 134 41.88 ┆           ┆           ┆ 9929134  │\n",
       "│ cfe0d…    ┆ 3d616…    ┆ PM        ┆ PM        ┆   ┆ 49871…    ┆           ┆           ┆ 41.88498 │\n",
       "│           ┆           ┆           ┆           ┆   ┆           ┆           ┆           ┆ 71…      │\n",
       "│ d3a30f861 ┆ 97ca0053c ┆ 10/23/201 ┆ 10/23/201 ┆ … ┆ POINT (-8 ┆ 41.892042 ┆ -87.63186 ┆ POINT    │\n",
       "│ 2e040663e ┆ 7078857a1 ┆ 6         ┆ 6         ┆   ┆ 7.6209929 ┆           ┆ 4         ┆ (-87.631 │\n",
       "│ 3908306f4 ┆ faa9bfa7b ┆ 07:45:00  ┆ 07:45:00  ┆   ┆ 134 41.88 ┆           ┆           ┆ 8639497  │\n",
       "│ 70efd…    ┆ ae198…    ┆ AM        ┆ AM        ┆   ┆ 49871…    ┆           ┆           ┆ 41.89204 │\n",
       "│           ┆           ┆           ┆           ┆   ┆           ┆           ┆           ┆ 21…      │\n",
       "│ 95178b9e5 ┆ f0b96329a ┆ 10/27/201 ┆ 10/27/201 ┆ … ┆ POINT (-8 ┆ 41.890922 ┆ -87.61886 ┆ POINT    │\n",
       "│ 2c980cca9 ┆ 7e3906bb2 ┆ 6         ┆ 6         ┆   ┆ 7.6570050 ┆           ┆ 8         ┆ (-87.618 │\n",
       "│ d837de35f ┆ e11b8af45 ┆ 08:45:00  ┆ 09:00:00  ┆   ┆ 27 41.879 ┆           ┆           ┆ 8683546  │\n",
       "│ 2eeae…    ┆ 6f130…    ┆ PM        ┆ PM        ┆   ┆ 06699…    ┆           ┆           ┆ 41.89092 │\n",
       "│           ┆           ┆           ┆           ┆   ┆           ┆           ┆           ┆ 20…      │\n",
       "│ b221f753f ┆ 11b7c42b4 ┆ 10/27/201 ┆ 10/27/201 ┆ … ┆ POINT (-8 ┆ 41.85935  ┆ -87.61735 ┆ POINT    │\n",
       "│ 4fce49e4c ┆ edd22871e ┆ 6         ┆ 6         ┆   ┆ 7.6221729 ┆           ┆ 8         ┆ (-87.617 │\n",
       "│ fccf07102 ┆ 20345b6d6 ┆ 08:30:00  ┆ 08:45:00  ┆   ┆ 369 41.87 ┆           ┆           ┆ 3580061  │\n",
       "│ de429…    ┆ 60674…    ┆ AM        ┆ AM        ┆   ┆ 06073…    ┆           ┆           ┆ 41.85934 │\n",
       "│           ┆           ┆           ┆           ┆   ┆           ┆           ┆           ┆ 97…      │\n",
       "│ d6d73a306 ┆ d49495206 ┆ 10/27/201 ┆ 10/27/201 ┆ … ┆ POINT (-8 ┆ 41.885281 ┆ -87.65723 ┆ POINT    │\n",
       "│ f0e4607fe ┆ 048d4831e ┆ 6         ┆ 6         ┆   ┆ 7.6414915 ┆           ┆ 3         ┆ (-87.657 │\n",
       "│ 3dc0f8767 ┆ 63e19601e ┆ 05:00:00  ┆ 05:15:00  ┆   ┆ 334 41.89 ┆           ┆           ┆ 2331997  │\n",
       "│ 5fb85…    ┆ 8a6af…    ┆ PM        ┆ PM        ┆   ┆ 79838…    ┆           ┆           ┆ 41.88528 │\n",
       "│           ┆           ┆           ┆           ┆   ┆           ┆           ┆           ┆ 13…      │\n",
       "└───────────┴───────────┴───────────┴───────────┴───┴───────────┴───────────┴───────────┴──────────┘"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "taxi_trips = (\n",
    "    pl.scan_csv('~/Downloads/Taxi_Trips.csv')\n",
    "    .collect(streaming=True)\n",
    ")\n",
    "taxi_trips.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 2)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>Payment Type</th><th>Trip Total</th></tr><tr><td>str</td><td>f64</td></tr></thead><tbody><tr><td>&quot;Prcard&quot;</td><td>4.0122e7</td></tr><tr><td>&quot;Credit Card&quot;</td><td>1.7685e9</td></tr><tr><td>&quot;Split&quot;</td><td>64668.43</td></tr><tr><td>&quot;No Charge&quot;</td><td>1.3089e7</td></tr><tr><td>&quot;Prepaid&quot;</td><td>40650.03</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 2)\n",
       "┌──────────────┬────────────┐\n",
       "│ Payment Type ┆ Trip Total │\n",
       "│ ---          ┆ ---        │\n",
       "│ str          ┆ f64        │\n",
       "╞══════════════╪════════════╡\n",
       "│ Prcard       ┆ 4.0122e7   │\n",
       "│ Credit Card  ┆ 1.7685e9   │\n",
       "│ Split        ┆ 64668.43   │\n",
       "│ No Charge    ┆ 1.3089e7   │\n",
       "│ Prepaid      ┆ 40650.03   │\n",
       "└──────────────┴────────────┘"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trip_total_by_pay_type = (\n",
    "    pl.scan_csv('~/Downloads/Taxi_Trips.csv')\n",
    "    .group_by('Payment Type')\n",
    "    .agg(pl.col('Trip Total').sum())\n",
    "    .collect()\n",
    ")\n",
    "trip_total_by_pay_type.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
